Passed
Push — master ( afef79...6b733e )
by Richard
09:09 queued 11s
created

WebhookController   A

Complexity

Total Complexity 4

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 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A publish() 0 19 4
1
<?php
2
3
namespace Riclep\Storyblok\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Cache;
7
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
8
9
class WebhookController
10
{
11
	/**
12
	 * Clears the cache when changing the publish state of a Story
13
	 *
14
	 * @param Request $request
15
	 * @return bool[]
16
	 */
17
	public function publish(Request $request)
18
	{
19
		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...
20
			throw new BadRequestHttpException('Header not set');
21
		}
22
23
		$signature = hash_hmac('sha1', $request->getContent(), config('storyblok.webhook_secret'));
24
25
		if ($request->header('webhook-signature') === $signature) {
26
			if (Cache::getStore() instanceof \Illuminate\Cache\TaggableStore) {
27
				Cache::tags('storyblok')->flush();
28
			} else {
29
				Cache::flush();
30
			}
31
32
			return ['success' => true];
33
		}
34
35
		throw new BadRequestHttpException('Signature has invalid format');
36
	}
37
}
38