Skip to content

Commit

Permalink
Update AmoCrmApiServiceProvider.php
Browse files Browse the repository at this point in the history
  • Loading branch information
bigperson authored May 17, 2018
1 parent d41ecde commit 5d6eb0a
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions src/AmoCrmApiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
*/
class AmoCrmApiServiceProvider extends ServiceProvider
{

protected $defer = true;

/**
* Local config file path.
*/
Expand Down Expand Up @@ -60,8 +63,8 @@ class AmoCrmApiServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->bootRequestHandler();
$this->bootServices();
$this->shareResources();
$this->mergeConfigFrom(self::CONFIG_PATH, 'amocrm-api');
}

/**
Expand All @@ -71,8 +74,9 @@ public function boot()
*/
public function register()
{
$this->shareResources();
$this->mergeConfigFrom(self::CONFIG_PATH, 'amocrm-api');
$this->registerRequestHandler();
$this->registerAuthorizationService();
$this->registerServices();
}

/**
Expand All @@ -87,13 +91,25 @@ private function shareResources(): void
}

/**
* @return void Boot Request Handler
* @return void Register Request Handler
*/
private function bootRequestHandler()
private function registerRequestHandler()
{
$this->app->singleton(RequestHandler::class, function ($app) {
$request = new \linkprofit\AmoCRM\RequestHandler();
$request->setSubdomain(config('amocrm.domain'));

return $request;
});
}

/**
* @return void Register and authorize Authorization Service
*/
private function registerAuthorizationService()
{
$this->app->singleton(AuthorizationService::class, function ($app) {
$request = $app->make(RequestHandler::class);
$connection = new \linkprofit\AmoCRM\entities\Authorization(
config('amocrm-api.login'),
config('amocrm-api.hash')
Expand All @@ -102,22 +118,33 @@ private function bootRequestHandler()
$authorization->add($connection);
$authorization->authorize();

return $request;
return $authorization;
});
}

/**
* @return void Boot all services
*/
private function bootServices()
private function registerServices()
{
foreach ($this->services as $service) {
$this->app->singleton($service, function ($app) use ($service) {
$this->app->bind($service, function ($app) use ($service) {
$request = $app->make(\linkprofit\AmoCRM\RequestHandler::class);
$authorization = $app->make(AuthorizationService::class);
$service = new $service($request);

return $service;
});
}
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [RequestHandler::class];
}
}

0 comments on commit 5d6eb0a

Please sign in to comment.