1 | <?php |
||
2 | |||
3 | namespace Spinen\ConnectWise\Laravel; |
||
4 | |||
5 | use GuzzleHttp\Client as Guzzle; |
||
6 | use Illuminate\Contracts\Foundation\Application; |
||
7 | use Illuminate\Support\ServiceProvider as LaravelServiceProvider; |
||
8 | use Spinen\ConnectWise\Api\Client; |
||
9 | use Spinen\ConnectWise\Api\Token; |
||
10 | use Spinen\ConnectWise\Exceptions\NoLoggedInUser; |
||
11 | use Spinen\ConnectWise\Support\ModelResolver; |
||
12 | |||
13 | /** |
||
14 | * Class ConnectWiseProvider |
||
15 | * |
||
16 | * @package Spinen\ConnectWise\Laravel |
||
17 | */ |
||
18 | class ServiceProvider extends LaravelServiceProvider |
||
19 | { |
||
20 | /** |
||
21 | * Indicates if loading of the provider is deferred. |
||
22 | * |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $defer = true; |
||
26 | |||
27 | /** |
||
28 | * Bootstrap the application services. |
||
29 | * |
||
30 | * @return void |
||
31 | */ |
||
32 | public function boot() |
||
33 | { |
||
34 | // |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Determine the member Id to use for API call |
||
39 | * |
||
40 | * Allow a default member id to use for the API calls if there is no logged in user. |
||
41 | * |
||
42 | * @return mixed |
||
43 | * @throws NoLoggedInUser |
||
44 | */ |
||
45 | protected function determineMemberId() |
||
46 | { |
||
47 | if ($this->app->auth->check()) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
48 | return $this->app->auth->user()->connect_wise_member_id; |
||
49 | } |
||
50 | |||
51 | if (!empty($this->app->config->get('services.connectwise.default_member_id'))) { |
||
0 ignored issues
–
show
|
|||
52 | return $this->app->config->get('services.connectwise.default_member_id'); |
||
53 | } |
||
54 | |||
55 | throw new NoLoggedInUser("There is not a currently logged in user."); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Register the application services. |
||
60 | * |
||
61 | * @return void |
||
62 | */ |
||
63 | public function register() |
||
64 | { |
||
65 | $this->registerToken(); |
||
66 | |||
67 | $this->registerClient(); |
||
68 | |||
69 | $this->app->alias(Client::class, 'connectwise'); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Register the client object |
||
74 | * |
||
75 | * A Client needs to have some properties set, so in Laravel, we are going to pull them from the configs. |
||
76 | */ |
||
77 | protected function registerClient() |
||
78 | { |
||
79 | $this->app->singleton( |
||
80 | Client::class, |
||
81 | function (Application $app) { |
||
82 | return (new Client( |
||
83 | $app->make(Token::class), |
||
84 | $app->make(Guzzle::class), |
||
85 | $app->make(ModelResolver::class) |
||
86 | ))->setClientId($app->config->get('services.connectwise.client_id')) |
||
0 ignored issues
–
show
|
|||
87 | ->setIntegrator($app->config->get('services.connectwise.integrator')) |
||
88 | ->setPassword($app->config->get('services.connectwise.password')) |
||
89 | ->setUrl($app->config->get('services.connectwise.url')) |
||
90 | ->setVersion($app->config->get('services.connectwise.version', '2019.4')); |
||
91 | } |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Get the services provided by the provider. |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | public function provides() |
||
101 | { |
||
102 | return [ |
||
103 | Client::class, |
||
104 | Token::class, |
||
105 | ]; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Register the token object |
||
110 | * |
||
111 | * For our setup, everyone's ConnectWise username (member id) is the front part of their email less the dot |
||
112 | * between the first & last name. |
||
113 | */ |
||
114 | protected function registerToken() |
||
115 | { |
||
116 | $this->app->singleton( |
||
117 | Token::class, |
||
118 | function (Application $app) { |
||
119 | return (new Token())->setCompanyId($app->config->get('services.connectwise.company_id')) |
||
0 ignored issues
–
show
|
|||
120 | ->setMemberId($this->determineMemberId()); |
||
121 | } |
||
122 | ); |
||
123 | } |
||
124 | } |
||
125 |