Support the ongoing development of Laravel.io →
posted 11 years ago
IOC

Hi,

I have an application design question.

Lets say I have multiple providers, that will send me delivery reports. I want to store the raw data (which differ from each provider) in a provider specific mysql table and transform it into a universal delivery report for the rest of my system. Without having to add the dependencies in the provider classes.

I was thinking something like this

class BaseDeliveryReport implements DeliveryReportInterface
{
	protected $repo;

	public function __construct(DeliveryReportRepositoryInterface $repo)
	{
		$this->repo = $repo;
	}

	public function create($attributes)
	{
		// Store delivery report etc...
	}
	// ...
}

My thought was to then do this:

class Provider1DeliveryReport extends BaseDeliveryReport
{
	protected function storeRawData($attributes) {
		// Provider specific stuff here
	}

	public function create($attributes)
	{
		$this->storeRawData($attributes);
		parent::create($attributes);
	}
}

$data = Input::all(); // or something...

$deliveryReport = new Provider1DeliveryReport();
$deliveryReport->create($data);

From my understanding I cannot do this, and I wonder how I can solve this without having to repeat the dependency injection in each providers constructor.

Thanks :)

Last updated 3 years ago.
0

The parent's constructor is not automatically fired when extending it. So the repo is not setup when you do

new Provider1DeliveryReport();

You can solve it by adding parent::__construct() to you Provider1DeliveryReport::__construct() but that's the same amount of work as injecting the repo.

Maybe somebody else has a great idea.

Last updated 3 years ago.
0

Thanks for your reply zenry.

Actually, the only way I get this working is to do like this on all provider classes:

public function __construct(DeliveryReportRepositoryInterface $repo)
{
	parent::__construct($repo);
}

Hopefully some skilled guy or gal out there has a solution to this :D

Thanks anyway!

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

rahaug rahaug Joined 18 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.