Passed
Push — master ( c65b3c...fcdaef )
by Jan
03:18
created

Controller::__invoke()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 25
ccs 15
cts 15
cp 1
rs 6.7272
cc 7
eloc 13
nc 10
nop 3
crap 7
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 Controller
11
{
12
13
	/**
14
	 * @var string
15
	 */
16
	private $secret;
17
18
	/**
19
	 * @var bool
20
	 */
21
	private $secured = FALSE;
22
23
	/**
24
	 * @var callable[]
25
	 */
26
	private $router;
27
28 6
	public function __construct(ContainerInterface $ci, Handler $handler)
29
	{
30 6
		$this->secret = $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 1
		$this->router['tag_push'] = function (array $event) use($handler) {
45 1
			$handler->handleTag($event);
46 1
		};
47 6
	}
48
49
50 6
	public function __invoke(Request $request, Response $response, $args)
51
	{
52 6
		$body = $request->getParsedBody();
53 6
		if ( ! isset($body['object_kind'])) {
54 1
			return $response->withStatus(500);
55
		}
56
57 5
		foreach ($request->getHeader('X-Gitlab-Token') as $secret) {
58 4
			if ($secret == $this->secret) { // allow cast
59 4
				$this->secured = TRUE;
60 4
			}
61 5
		}
62
63 5
		if ($this->secret !== NULL && ! $this->secured) {
64 1
			return $response->withStatus(403);
65
		}
66
67 4
		if (isset($this->router[$body['object_kind']])) {
68 3
			$this->router[$body['object_kind']]($body);
69 3
			return $response->withStatus(200);
70
		}
71
72
73 1
		return $response->withStatus(404);
74
	}
75
76
}
77