Passed
Push — master ( 4fcf6f...e67987 )
by Jan
03:06
created

HookController::isSecured()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 6
nc 6
nop 1
crap 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A HookController::isHandled() 0 5 1
1
<?php
2
3
namespace App;
4
5
use Interop\Container\ContainerInterface;
6
use Slim\Http\Request;
7
use Slim\Http\Response;
8
9
10
final class HookController
11
{
12
13
	use SecuredTrait;
14
15
	const SECRET_HEADER = 'X-Gitlab-Token';
16
17
	/**
18
	 * @var string
19
	 */
20
	private $secret;
21
22
	/**
23
	 * @var callable[]
24
	 */
25
	private $router;
26
27
28 8
	public function __construct(ContainerInterface $ci, HookHandler $handler)
29
	{
30 8
		$this->secret = (string) $ci->get('settings')['secret'];
31
32
		$this->router['pipeline'] = function (array $event) use($handler) {
33 1
			foreach ($event['builds'] as $build) {
34 1
				if ($build['stage'] === 'deploy') {
35 1
					$handler->handleDeploy($event, $build);
36 1
				}
37 1
			}
38 1
		};
39
40
		$this->router['push'] = function (array $event) use($handler) {
41 1
			$handler->handlePush($event);
42 1
		};
43
44 3
		$this->router['tag_push'] = function (array $event) use($handler) {
45 3
			$handler->handleTag($event);
46 3
		};
47 8
	}
48
49
50 8
	public function __invoke(Request $request, Response $response, array $args)
51
	{
52 8
		if ( ! $this->isValid($request)) {
53 1
			return $response->withStatus(500);
54
		}
55
56 7
		if ( ! $this->isSecured($request)) {
57 1
			return $response->withStatus(403);
58
		}
59
60 6
		if ($this->isHandled($request)) {
61 5
			$this->handle($request);
62 5
			return $response->withStatus(200);
63
		}
64
65 1
		return $response->withStatus(404);
66
	}
67
68
69
	/**
70
	 * @param Request $request
71
	 * @return bool
72
	 */
73 8
	private function isValid(Request $request)
74
	{
75 8
		$body = $request->getParsedBody();
76 8
		if ( ! isset($body['object_kind'])) {
77 1
			return FALSE;
78
		}
79
80 7
		return TRUE;
81
	}
82
83
84
	/**
85
	 * @param Request $request
86
	 * @return bool
87
	 */
88 6
	private function isHandled(Request $request)
89
	{
90 6
		$body = $request->getParsedBody();
91 6
		return isset($this->router[$body['object_kind']]);
92
	}
93
94
95
	/**
96
	 * @param Request $request
97
	 */
98 5
	private function handle(Request $request)
99
	{
100 5
		$body = $request->getParsedBody();
101 5
		$this->router[$body['object_kind']]($body);
102 5
	}
103
104
}
105