WebhookController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 27
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A publish() 0 19 6
1
<?php
2
3
namespace Riclep\Storyblok\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Cache;
7
use Riclep\Storyblok\Events\StoryblokPublished;
8
use Riclep\Storyblok\Events\StoryblokUnpublished;
9
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
10
11
class WebhookController
12
{
13
	/**
14
	 * Clears the cache when changing the publish state of a Story
15
	 *
16
	 * @param Request $request
17
	 * @return bool[]
18
	 */
19
	public function publish(Request $request): array
20
	{
21
		if ($request->headers->get('webhook-signature') === null) {
0 ignored issues
show
introduced by
The condition $request->headers->get('...ok-signature') === null is always false.
Loading history...
22
			throw new BadRequestHttpException('Header not set');
23
		}
24
25
		$signature = hash_hmac('sha1', $request->getContent(), config('storyblok.webhook_secret'));
26
27
		if ($request->header('webhook-signature') === $signature) {
28
			if ($request->all()['action'] === 'published') {
29
				StoryblokPublished::dispatch($request->all());
30
			} else if ($request->all()['action'] === 'unpublished' || $request->all()['action'] === 'deleted') {
31
				StoryblokUnpublished::dispatch($request->all());
32
			}
33
34
			return ['success' => true];
35
		}
36
37
		throw new BadRequestHttpException('Signature has invalid format');
38
	}
39
}
40