1 | <?php |
||
61 | abstract class AbstractApi |
||
62 | { |
||
63 | /** |
||
64 | * @var \Xabbuh\PandaClient\Transformer\TransformerRegistryInterface |
||
65 | */ |
||
66 | protected $transformers; |
||
67 | |||
68 | /** |
||
69 | * @var \Xabbuh\PandaClient\Api\AccountManagerInterface |
||
70 | */ |
||
71 | protected $accountManager; |
||
72 | |||
73 | /** |
||
74 | * @var \Xabbuh\PandaClient\Api\CloudManagerInterface |
||
75 | */ |
||
76 | protected $cloudManager; |
||
77 | |||
78 | 8 | final public function __construct(array $config) |
|
79 | { |
||
80 | // create the transformation layer |
||
81 | 8 | $this->transformers = $this->createTransformerRegistry(); |
|
82 | 8 | $this->transformers->setCloudTransformer($this->createCloudTransformer()); |
|
83 | 8 | $this->transformers->setEncodingTransformer($this->createEncodingTransformer()); |
|
84 | 8 | $this->transformers->setNotificationsTransformer($this->createNotificationsTransformer()); |
|
85 | 8 | $this->transformers->setProfileTransformer($this->createProfileTransformer()); |
|
86 | 8 | $this->transformers->setVideoTransformer($this->createVideoTransformer()); |
|
87 | |||
88 | // register the accounts |
||
89 | 8 | $this->accountManager = $this->createAccountManager(); |
|
90 | 8 | $this->accountManager->setDefaultAccount( |
|
91 | 8 | isset($config['default_account']) ? $config['default_account'] : 'default' |
|
92 | ); |
||
93 | 8 | $this->processAccountConfig($config); |
|
94 | |||
95 | // register the clouds |
||
96 | 7 | $this->cloudManager = $this->createCloudManager(); |
|
97 | 7 | $this->cloudManager->setDefaultCloud( |
|
98 | 7 | isset($config['default_cloud']) ? $config['default_cloud'] : 'default' |
|
99 | ); |
||
100 | 7 | $this->processCloudConfig($config); |
|
101 | 5 | } |
|
102 | |||
103 | /** |
||
104 | * @param array $config |
||
105 | * |
||
106 | * @throws \InvalidArgumentException |
||
107 | */ |
||
108 | 8 | protected function processAccountConfig(array $config) |
|
109 | { |
||
110 | 8 | if (!isset($config['accounts']) || !is_array($config['accounts'])) { |
|
111 | 1 | throw new \InvalidArgumentException('No account configuration given.'); |
|
112 | } |
||
113 | |||
114 | 7 | foreach ($config['accounts'] as $name => $accountConfig) { |
|
115 | 7 | $this->validateMandatoryOptions($accountConfig, 'account', $name, array('access_key', 'secret_key', 'api_host')); |
|
116 | |||
117 | 7 | $this->accountManager->registerAccount( |
|
118 | 7 | $name, |
|
119 | 7 | $this->createAccount( |
|
120 | 7 | $accountConfig['access_key'], |
|
121 | 7 | $accountConfig['secret_key'], |
|
122 | 7 | $accountConfig['api_host'] |
|
123 | ) |
||
124 | ); |
||
125 | } |
||
126 | 7 | } |
|
127 | |||
128 | /** |
||
129 | * @param array $config |
||
130 | * |
||
131 | * @throws \InvalidArgumentException |
||
132 | */ |
||
133 | 7 | protected function processCloudConfig(array $config) |
|
134 | { |
||
135 | 7 | if (!isset($config['clouds']) || !is_array($config['clouds'])) { |
|
136 | 1 | throw new \InvalidArgumentException('No cloud configuration given.'); |
|
137 | } |
||
138 | |||
139 | 6 | foreach ($config['clouds'] as $name => $cloudConfig) { |
|
140 | 6 | $this->validateMandatoryOptions($cloudConfig, 'cloud', $name, array('id', 'account')); |
|
141 | |||
142 | try { |
||
143 | 5 | $account = $this->accountManager->getAccount($cloudConfig['account']); |
|
144 | } catch (\InvalidArgumentException $e) { |
||
145 | throw new \InvalidArgumentException( |
||
146 | sprintf('Invalid account %s for cloud %s', $name, $cloudConfig['account']) |
||
147 | ); |
||
148 | } |
||
149 | |||
150 | 5 | $httpClient = $this->createHttpClient($account, $cloudConfig['id']); |
|
151 | 5 | $cloud = $this->createCloud(); |
|
152 | 5 | $cloud->setHttpClient($httpClient); |
|
153 | 5 | $cloud->setTransformers($this->transformers); |
|
154 | 5 | $this->cloudManager->registerCloud($name, $cloud); |
|
155 | } |
||
156 | 5 | } |
|
157 | |||
158 | /** |
||
159 | * Creates the {@link AccountManager} that manages the configured accounts. |
||
160 | * |
||
161 | * @return \Xabbuh\PandaClient\Api\AccountManagerInterface |
||
162 | */ |
||
163 | abstract protected function createAccountManager(); |
||
164 | |||
165 | /** |
||
166 | * Creates an {@link Account} for the given configuration. |
||
167 | * |
||
168 | * @param string $accessKey The access key |
||
169 | * @param string $secretKey The secret key |
||
170 | * @param string $apiHost The API host |
||
171 | * |
||
172 | * @return \Xabbuh\PandaClient\Api\Account The created account |
||
173 | */ |
||
174 | 7 | protected function createAccount($accessKey, $secretKey, $apiHost) |
|
175 | { |
||
176 | 7 | return new Account($accessKey, $secretKey, $apiHost); |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * Creates the {@link CloudManager} that manages the configured clouds. |
||
181 | * |
||
182 | * @return \Xabbuh\PandaClient\Api\CloudManagerInterface |
||
183 | */ |
||
184 | abstract protected function createCloudManager(); |
||
185 | |||
186 | /** |
||
187 | * Creates the {@link HttpClientInterface HttpClient} that performs the HTTP |
||
188 | * requests. |
||
189 | * |
||
190 | * @param Account $account Account authentication data |
||
191 | * @param string $cloudId Id of the cloud to access |
||
192 | * |
||
193 | * @return \Xabbuh\PandaClient\Api\HttpClientInterface |
||
194 | */ |
||
195 | abstract protected function createHttpClient(Account $account, $cloudId); |
||
196 | |||
197 | /** |
||
198 | * Creates a {@link Cloud} instances that maps Panda API REST endpoints to |
||
199 | * method calls. |
||
200 | * |
||
201 | * @return \Xabbuh\PandaClient\Api\CloudInterface |
||
202 | */ |
||
203 | abstract protected function createCloud(); |
||
204 | |||
205 | /** |
||
206 | * Creates the {@link TransformerRegistryInterface registry} that manages |
||
207 | * the keeps track of the different transformers. |
||
208 | * |
||
209 | * @return \Xabbuh\PandaClient\Transformer\TransformerRegistryInterface |
||
210 | */ |
||
211 | abstract protected function createTransformerRegistry(); |
||
212 | |||
213 | /** |
||
214 | * Creates a transformer that transforms between the native API format and |
||
215 | * {@link Cloud} instances an vice versa. |
||
216 | * |
||
217 | * @return \Xabbuh\PandaClient\Transformer\CloudTransformerInterface |
||
218 | */ |
||
219 | abstract protected function createCloudTransformer(); |
||
220 | |||
221 | /** |
||
222 | * Creates a transformer that transforms between the native API format and |
||
223 | * {@link Encoding} instances an vice versa. |
||
224 | * |
||
225 | * @return \Xabbuh\PandaClient\Transformer\EncodingTransformerInterface |
||
226 | */ |
||
227 | abstract protected function createEncodingTransformer(); |
||
228 | |||
229 | /** |
||
230 | * Creates a transformer that transforms between the native API format and |
||
231 | * {@link Notifications} instances an vice versa. |
||
232 | * |
||
233 | * @return \Xabbuh\PandaClient\Transformer\NotificationsTransformerInterface |
||
234 | */ |
||
235 | abstract protected function createNotificationsTransformer(); |
||
236 | |||
237 | /** |
||
238 | * Creates a transformer that transforms between the native API format and |
||
239 | * {@link Profile} instances an vice versa. |
||
240 | * |
||
241 | * @return \Xabbuh\PandaClient\Transformer\ProfileTransformerInterface |
||
242 | */ |
||
243 | abstract protected function createProfileTransformer(); |
||
244 | |||
245 | /** |
||
246 | * Creates a transformer that transforms between the native API format and |
||
247 | * {@link Video} instances an vice versa. |
||
248 | * |
||
249 | * @return \Xabbuh\PandaClient\Transformer\VideoTransformerInterface |
||
250 | */ |
||
251 | abstract protected function createVideoTransformer(); |
||
252 | |||
253 | /** |
||
254 | * Returns the created {@link AccountManagerInterface AccountManager}. |
||
255 | * |
||
256 | * @return \Xabbuh\PandaClient\Api\AccountManagerInterface |
||
257 | */ |
||
258 | 2 | public function getAccountManager() |
|
262 | |||
263 | /** |
||
264 | * Returns the created {@link CloudManagerInterface CloudManager}. |
||
265 | * |
||
266 | * @return \Xabbuh\PandaClient\Api\CloudManagerInterface |
||
267 | */ |
||
268 | 2 | public function getCloudManager() |
|
272 | |||
273 | /** |
||
274 | * Returns a {@link Cloud} by its configured name. |
||
275 | * |
||
276 | * @param string $name The configured name of the Cloud |
||
277 | * |
||
278 | * @return \Xabbuh\PandaClient\Api\CloudInterface |
||
279 | */ |
||
280 | 2 | public function getCloud($name) |
|
284 | |||
285 | /** |
||
286 | * Creates a cloud. |
||
287 | * |
||
288 | * @param string $accessKey The access key |
||
289 | * @param string $secretKey The secret key |
||
290 | * @param string $apiHost The api host |
||
291 | * @param string $cloudId The cloud id |
||
292 | * |
||
293 | * @return \Xabbuh\PandaClient\Api\CloudInterface The cloud |
||
294 | */ |
||
295 | public static function getCloudInstance($accessKey, $secretKey, $apiHost, $cloudId) |
||
318 | |||
319 | 7 | private function validateMandatoryOptions(array $config, $section, $name, array $options) |
|
329 | } |
||
330 |