Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
30 / 30 |
GoogleBaseService | |
100.00% |
1 / 1 |
|
100.00% |
10 / 10 |
18 | |
100.00% |
30 / 30 |
make | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
__get | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
__call | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
createService | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
service | |
100.00% |
1 / 1 |
4 | |
100.00% |
6 / 6 |
|||
withPermission | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
withMessage | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
getOption | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
setOption | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\GoogleApiWrapper\Services; |
4 | |
5 | use Exception; |
6 | use Google\Service\Drive\Drive; |
7 | use Illuminate\Support\Traits\Macroable; |
8 | use Qmp\Laravel\GoogleApiWrapper\Client\GoogleClientWrapper; |
9 | use Qmp\Laravel\GoogleApiWrapper\Factory\GoogleServiceFactory; |
10 | use Qmp\Laravel\GoogleApiWrapper\GoogleApiWrapper; |
11 | /** |
12 | * @method array spreadsheetList() |
13 | */ |
14 | abstract class GoogleBaseService |
15 | { |
16 | use Macroable { |
17 | __call as macroCall; |
18 | } |
19 | |
20 | /** |
21 | * Undocumented variable |
22 | * |
23 | * @var GoogleClientWrapper |
24 | */ |
25 | protected $client; |
26 | |
27 | /** |
28 | * Undocumented variable |
29 | * |
30 | * @var Object |
31 | */ |
32 | protected $service; |
33 | |
34 | /** |
35 | * Undocumented variable |
36 | * |
37 | * @var string |
38 | */ |
39 | protected $serviceFileClass; |
40 | |
41 | /** |
42 | * Undocumented variable |
43 | * |
44 | * @var String |
45 | */ |
46 | protected $serviceClass; |
47 | |
48 | /** |
49 | * Undocumented variable |
50 | * |
51 | * @var GoogleApiWrapper |
52 | */ |
53 | protected $apiWrapper; |
54 | |
55 | |
56 | /** |
57 | * Undocumented function |
58 | * |
59 | * @param GoogleClientWrapper $client |
60 | * @param GoogleApiWrapper|null $apiWrapper |
61 | * @return self |
62 | */ |
63 | public static function make(GoogleClientWrapper $client, GoogleApiWrapper $apiWrapper = null): self |
64 | { |
65 | return new static($client, $apiWrapper); |
66 | } |
67 | |
68 | |
69 | /** |
70 | * Undocumented function |
71 | * |
72 | * @param GoogleClientWrapper $client |
73 | * @param GoogleApiWrapper|null $apiWrapper |
74 | */ |
75 | public function __construct(GoogleClientWrapper $client, GoogleApiWrapper $apiWrapper = null) |
76 | { |
77 | $this->client = $client; |
78 | $this->apiWrapper = $apiWrapper; |
79 | $this->service = $this->createService(); |
80 | } |
81 | |
82 | |
83 | /** |
84 | * @param string $property |
85 | * @return mixed |
86 | * @throws \InvalidArgumentException |
87 | */ |
88 | public function __get(string $property) |
89 | { |
90 | if (property_exists($this->service, $property)) { |
91 | return $this->service->{$property}; |
92 | } |
93 | |
94 | throw new \InvalidArgumentException(sprintf('Property [%s] does not exist.', $property)); |
95 | } |
96 | |
97 | /** |
98 | * Magic call method. |
99 | * |
100 | * @param string $method |
101 | * @param array $parameters |
102 | * |
103 | * @return mixed |
104 | * @throws \BadMethodCallException |
105 | */ |
106 | public function __call(string $method, array $parameters) |
107 | { |
108 | if (method_exists($this->service, $method)) { |
109 | return $this->service->{$method}(...array_values($parameters)); |
110 | } |
111 | |
112 | return $this->macroCall($method, $parameters); |
113 | } |
114 | /** |
115 | * Undocumented function |
116 | * |
117 | * @return Object |
118 | */ |
119 | protected function createService() |
120 | { |
121 | if ($this->serviceClass) { |
122 | return new $this->serviceClass($this->client->getClient()); |
123 | } |
124 | |
125 | throw new Exception('$serviceClass must be set !'); |
126 | } |
127 | |
128 | /** |
129 | * Undocumented function |
130 | * |
131 | * @param string $serviceName |
132 | * @return GoogleBaseService |
133 | */ |
134 | protected function service(string $serviceName = null): GoogleBaseService |
135 | { |
136 | $currentService = explode('\\', $this->serviceClass); |
137 | |
138 | if (!$serviceName || $serviceName === strtolower(end($currentService))) { |
139 | return $this; |
140 | } |
141 | |
142 | if ($this->apiWrapper) { |
143 | return $this->apiWrapper->service($serviceName); |
144 | } |
145 | |
146 | return GoogleServiceFactory::make($serviceName, $this->client); |
147 | } |
148 | |
149 | /** |
150 | * Undocumented function |
151 | * |
152 | * @param array $permission |
153 | * @return self |
154 | */ |
155 | public function withPermission(array $permission): self |
156 | { |
157 | $this->setOption('permission', $permission); |
158 | return $this; |
159 | } |
160 | |
161 | /** |
162 | * Undocumented function |
163 | * |
164 | * @param string $message |
165 | * @return self |
166 | */ |
167 | public function withMessage(string $message): self |
168 | { |
169 | $this->setOption('message', $message); |
170 | return $this; |
171 | } |
172 | |
173 | /** |
174 | * Undocumented function |
175 | * |
176 | * @param string $key |
177 | * @return void |
178 | */ |
179 | public function getOption($key) |
180 | { |
181 | if ($this->apiWrapper) { |
182 | return $this->apiWrapper->getOption($key); |
183 | } |
184 | return null; |
185 | } |
186 | |
187 | /** |
188 | * Undocumented function |
189 | * |
190 | * @param string $key |
191 | * @param mixed $value |
192 | * @return void |
193 | */ |
194 | public function setOption($key, $value) |
195 | { |
196 | if ($this->apiWrapper) { |
197 | $this->apiWrapper->setOption($key, $value); |
198 | } |
199 | } |
200 | } |