Skip to content

Instantly share code, notes, and snippets.

@jesseschutt
Last active August 29, 2015 14:13
Show Gist options
  • Save jesseschutt/6a78d9ee1067c8d6b598 to your computer and use it in GitHub Desktop.
Save jesseschutt/6a78d9ee1067c8d6b598 to your computer and use it in GitHub Desktop.
DI Sample
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Company;
use App\Repositories\Company\EloquentCompany;
class CompanyServiceProvider extends ServiceProvider {
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind('App\Repositories\Company\CompanyInterface', function($app)
{
return new EloquentCompany(new Company,
$app->make('App\Repositories\Network\NetworkInterface'));
});
}
}
<?php namespace App\Repositories\Company;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Rhumsaa\Uuid\Uuid;
use App\Repositories\DbRepository;
use App\Repositories\Company\CompanyInterface;
class EloquentCompany extends DbRepository implements CompanyInterface {
/**
* @var Model
*/
private $company;
private $network;
public function __construct(
Model $company,
NetworkInterface $network)
{
$this->company = $company;
$this->network = $network;
}
/**
* @param $request
* @param $user
* @return static
*/
public function save($request, $user)
{
// lookup the users network
$network = $this->network->getByUserId($user->id);
$request->merge([
'status_id' => '1', // The default status (Unknown Status) due to primary key constraint
'uuid' => Uuid::uuid4()->toString(),
]);
$company = $this->company->create($request->all());
// Need to associate the newly created company with the users network
$company->networks()->attach($user->id, ['user_tags' => $request->tags]);
return $company;
}
}
<?php namespace App\Repositories\Network;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Rhumsaa\Uuid\Uuid;
use App\Repositories\DbRepository;
use App\Repositories\Network\NetworkInterface;
class EloquentNetwork extends DbRepository implements NetworkInterface {
/**
* @var Model
*/
protected $network;
protected $company;
public function __construct(
Model $network,
CompanyInterface $company
)
{
$this->network = $network;
$this->company = $company;
}
public function addCompanyByUuid($uuid, $user)
{
$company = $this->company->findByUuid($uuid);
$network = $this->findByUserId($user->id);
if( $network->companies()->get()->contains($company->id))
{
return FALSE;
}
$network->companies()->attach($company->id);
return $company;
}
}
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Network;
use App\Repositories\Network\EloquentNetwork;
class NetworkServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind('App\Repositories\Network\NetworkInterface', function ($app)
{
return new EloquentNetwork(new Network,
$app->make('App\Repositories\Company\CompanyInterface'));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment