Completed
Push — master ( d41ecd...5d6eb0 )
by Anton
02:35
created

AmoCrmApiServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\ServiceProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use linkprofit\AmoCRM\RequestHandler;
15
use linkprofit\AmoCRM\services\AccountService;
16
use linkprofit\AmoCRM\services\CatalogElementService;
0 ignored issues
show
Bug introduced by
The type linkprofit\AmoCRM\services\CatalogElementService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
34
    protected $defer = true;
35
36
    /**
37
     * Local config file path.
38
     */
39
    private const CONFIG_PATH = __DIR__.'/../config/amocrm-api.php';
40
41
    /**
42
     * @var array List of services
43
     */
44
    protected $services = [
45
        AccountService::class,
46
        CatalogElementService::class,
47
        CatalogService::class,
48
        CompanyService::class,
49
        ContactService::class,
50
        CustomerService::class,
51
        FieldService::class,
52
        LeadService::class,
53
        NoteService::class,
54
        PipelineService::class,
55
        TaskService::class,
56
        TaskTypeService::class,
57
    ];
58
59
    /**
60
     * Bootstrap the application services.
61
     *
62
     * @return void
63
     */
64
    public function boot()
65
    {
66
        $this->shareResources();
67
        $this->mergeConfigFrom(self::CONFIG_PATH, 'amocrm-api');
68
    }
69
70
    /**
71
     * Register the application services.
72
     *
73
     * @return void
74
     */
75
    public function register()
76
    {
77
        $this->registerRequestHandler();
78
        $this->registerAuthorizationService();
79
        $this->registerServices();
80
    }
81
82
    /**
83
     * @return void
84
     */
85
    private function shareResources(): void
86
    {
87
        $publishes = [
88
            self::CONFIG_PATH => \config_path('amocrm-api.php'),
0 ignored issues
show
Bug introduced by
The function config_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
            self::CONFIG_PATH => /** @scrutinizer ignore-call */ \config_path('amocrm-api.php'),
Loading history...
89
        ];
90
        $this->publishes($publishes, 'amocrm-api');
91
    }
92
93
    /**
94
     * @return void Register Request Handler
95
     */
96
    private function registerRequestHandler()
97
    {
98
        $this->app->singleton(RequestHandler::class, function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

98
        $this->app->singleton(RequestHandler::class, function (/** @scrutinizer ignore-unused */ $app) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
            $request = new \linkprofit\AmoCRM\RequestHandler();
100
            $request->setSubdomain(config('amocrm.domain'));
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
            $request->setSubdomain(/** @scrutinizer ignore-call */ config('amocrm.domain'));
Loading history...
101
102
            return $request;
103
        });
104
    }
105
106
    /**
107
     * @return void Register and authorize Authorization Service
108
     */
109
    private function registerAuthorizationService()
110
    {
111
        $this->app->singleton(AuthorizationService::class, function ($app) {
0 ignored issues
show
Bug introduced by
The type Bigperson\AmoCrmApi\AuthorizationService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
112
            $request = $app->make(RequestHandler::class);
113
            $connection = new \linkprofit\AmoCRM\entities\Authorization(
114
                config('amocrm-api.login'),
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
                /** @scrutinizer ignore-call */ 
115
                config('amocrm-api.login'),
Loading history...
115
                config('amocrm-api.hash')
116
            );
117
            $authorization = new \linkprofit\AmoCRM\services\AuthorizationService($request);
118
            $authorization->add($connection);
119
            $authorization->authorize();
120
121
            return $authorization;
122
        });
123
    }
124
125
    /**
126
     * @return void Boot all services
127
     */
128
    private function registerServices()
129
    {
130
        foreach ($this->services as $service) {
131
            $this->app->bind($service, function ($app) use ($service) {
132
                $request = $app->make(\linkprofit\AmoCRM\RequestHandler::class);
133
                $authorization = $app->make(AuthorizationService::class);
0 ignored issues
show
Unused Code introduced by
The assignment to $authorization is dead and can be removed.
Loading history...
134
                $service = new $service($request);
135
136
                return $service;
137
            });
138
        }
139
    }
140
141
    /**
142
     * Get the services provided by the provider.
143
     *
144
     * @return array
145
     */
146
    public function provides()
147
    {
148
        return [RequestHandler::class];
149
    }
150
}
151