| Total Complexity | 10 |
| Total Lines | 56 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 0 | ||
| 1 | <?php |
||
| 7 | class Manager |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Handle Contentful webhook for given headers and payload. |
||
| 11 | * |
||
| 12 | * @param array $headers |
||
| 13 | * @param array $payload |
||
| 14 | * @param boolean $isPreview |
||
| 15 | * @return array |
||
| 16 | */ |
||
| 17 | public function handle(array $headers, array $payload, bool $isPreview = false): array |
||
| 18 | { |
||
| 19 | if (! isset($headers['x-contentful-topic'])) { |
||
| 20 | return $this->response('Page not found', 404); |
||
| 21 | } |
||
| 22 | |||
| 23 | $topics = explode('.', $headers['x-contentful-topic'][0]); |
||
| 24 | if ($topics[0] !== 'ContentManagement') { |
||
| 25 | return $this->response('Page not found', 404); |
||
| 26 | } |
||
| 27 | |||
| 28 | switch ($topics[1]) { |
||
| 29 | case 'Asset': |
||
| 30 | $handler = new AssetHandler; |
||
| 31 | break; |
||
| 32 | case 'Entry': |
||
| 33 | $handler = new EntryHandler; |
||
| 34 | break; |
||
| 35 | case 'ContentType': |
||
| 36 | default: |
||
| 37 | $handler = null; |
||
| 38 | } |
||
| 39 | |||
| 40 | if (! empty($handler)) { |
||
| 41 | try { |
||
| 42 | $handler->handle($topics[2], $payload, $isPreview); |
||
| 43 | } catch (Exception $e) { |
||
| 44 | return $this->response($e->getMessage(), 500); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | return $this->response(); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Return normalized service response signature. |
||
| 53 | * |
||
| 54 | * @param string $message |
||
| 55 | * @param integer $status |
||
| 56 | * @return array |
||
| 57 | */ |
||
| 58 | private function response(string $message = '', int $status = 200): array |
||
| 63 | ]; |
||
| 64 | } |
||
| 66 |