HookController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 11
c 4
b 1
f 1
lcom 1
cbo 5
dl 0
loc 90
ccs 35
cts 35
cp 1
rs 10

5 Methods

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