bigperson /
amocrm-api-laravel
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | /** |
||
| 5 | * @author Anton Kartsev <[email protected]> |
||
| 6 | * |
||
| 7 | * For the full copyright and license information, please view the LICENSE |
||
| 8 | * file that was distributed with this source code. |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Bigperson\AmoCrmApi; |
||
| 12 | |||
| 13 | use Illuminate\Support\ServiceProvider; |
||
| 14 | use linkprofit\AmoCRM\RequestHandler; |
||
| 15 | use linkprofit\AmoCRM\services\AccountService; |
||
| 16 | use linkprofit\AmoCRM\services\CatalogElementService; |
||
| 17 | use linkprofit\AmoCRM\services\CatalogService; |
||
| 18 | use linkprofit\AmoCRM\services\CompanyService; |
||
| 19 | use linkprofit\AmoCRM\services\ContactService; |
||
| 20 | use linkprofit\AmoCRM\services\CustomerService; |
||
| 21 | use linkprofit\AmoCRM\services\FieldService; |
||
| 22 | use linkprofit\AmoCRM\services\LeadService; |
||
| 23 | use linkprofit\AmoCRM\services\NoteService; |
||
| 24 | use linkprofit\AmoCRM\services\PipelineService; |
||
| 25 | use linkprofit\AmoCRM\services\TaskService; |
||
| 26 | use linkprofit\AmoCRM\services\TaskTypeService; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Class AmoCrmApiServiceProvider. |
||
| 30 | */ |
||
| 31 | class AmoCrmApiServiceProvider extends ServiceProvider |
||
| 32 | { |
||
| 33 | protected $defer = true; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Local config file path. |
||
| 37 | */ |
||
| 38 | private const CONFIG_PATH = __DIR__.'/../config/amocrm-api.php'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array List of services |
||
| 42 | */ |
||
| 43 | protected $services = [ |
||
| 44 | AccountService::class, |
||
| 45 | CatalogElementService::class, |
||
| 46 | CatalogService::class, |
||
| 47 | CompanyService::class, |
||
| 48 | ContactService::class, |
||
| 49 | CustomerService::class, |
||
| 50 | FieldService::class, |
||
| 51 | LeadService::class, |
||
| 52 | NoteService::class, |
||
| 53 | PipelineService::class, |
||
| 54 | TaskService::class, |
||
| 55 | TaskTypeService::class, |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Bootstrap the application services. |
||
| 60 | * |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | public function boot() |
||
| 64 | { |
||
| 65 | $this->shareResources(); |
||
| 66 | $this->mergeConfigFrom(self::CONFIG_PATH, 'amocrm-api'); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Register the application services. |
||
| 71 | * |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | public function register() |
||
| 75 | { |
||
| 76 | $this->registerRequestHandler(); |
||
| 77 | $this->registerAuthorizationService(); |
||
| 78 | $this->registerServices(); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return void |
||
| 83 | */ |
||
| 84 | private function shareResources(): void |
||
| 85 | { |
||
| 86 | $publishes = [ |
||
| 87 | self::CONFIG_PATH => \config_path('amocrm-api.php'), |
||
| 88 | ]; |
||
| 89 | $this->publishes($publishes, 'amocrm-api'); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return void Register Request Handler |
||
| 94 | */ |
||
| 95 | private function registerRequestHandler() |
||
| 96 | { |
||
| 97 | $this->app->singleton(RequestHandler::class, function ($app) { |
||
|
0 ignored issues
–
show
|
|||
| 98 | $request = new \linkprofit\AmoCRM\RequestHandler(); |
||
| 99 | $request->setSubdomain(config('amocrm-api.domain')); |
||
| 100 | |||
| 101 | return $request; |
||
| 102 | }); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return void Register and authorize Authorization Service |
||
| 107 | */ |
||
| 108 | private function registerAuthorizationService() |
||
| 109 | { |
||
| 110 | $this->app->singleton(AuthorizationService::class, function ($app) { |
||
| 111 | $request = $app->make(RequestHandler::class); |
||
| 112 | $connection = new \linkprofit\AmoCRM\entities\Authorization( |
||
| 113 | config('amocrm-api.login'), |
||
| 114 | config('amocrm-api.hash') |
||
| 115 | ); |
||
| 116 | $authorization = new \linkprofit\AmoCRM\services\AuthorizationService($request); |
||
| 117 | $authorization->add($connection); |
||
| 118 | if ($this->app->environment() !== 'testing') { |
||
| 119 | $authorization->authorize(); |
||
| 120 | } |
||
| 121 | |||
| 122 | return $authorization; |
||
| 123 | }); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return void Boot all services |
||
| 128 | */ |
||
| 129 | private function registerServices() |
||
| 130 | { |
||
| 131 | foreach ($this->services as $service) { |
||
| 132 | $this->app->bind($service, function ($app) use ($service) { |
||
| 133 | $request = $app->make(\linkprofit\AmoCRM\RequestHandler::class); |
||
| 134 | $authorization = $app->make(AuthorizationService::class); |
||
|
0 ignored issues
–
show
|
|||
| 135 | $service = new $service($request); |
||
| 136 | |||
| 137 | return $service; |
||
| 138 | }); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get the services provided by the provider. |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | public function provides() |
||
| 148 | { |
||
| 149 | return [RequestHandler::class]; |
||
| 150 | } |
||
| 151 | } |
||
| 152 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.