Last active
August 29, 2015 14:13
-
-
Save jesseschutt/6a78d9ee1067c8d6b598 to your computer and use it in GitHub Desktop.
DI Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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