Passed
Branch master (c65b3c)
by Jan
05:40 queued 01:37
created

Controller::__invoke()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0145

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
ccs 14
cts 15
cp 0.9333
rs 6.7272
cc 7
eloc 12
nc 10
nop 3
crap 7.0145
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 4
	public function __construct(ContainerInterface $ci, Handler $handler)
29
	{
30 4
		$this->ci = $ci;
0 ignored issues
show
Bug introduced by
The property ci does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31 4
		$this->secret = $ci->get('settings')['secret'];
32 4
		$this->scripts = $ci->get('scripts');
0 ignored issues
show
Bug introduced by
The property scripts does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
34
		$this->router['pipeline'] = function (array $event) use($handler) {
35 1
			foreach ($event['builds'] as $build) {
36 1
				if ($build['stage'] === 'deploy') {
37 1
					$handler->handleDeploy($event, $build);
38 1
				}
39 1
			}
40 1
		};
41
42
		$this->router['push'] = function (array $event) use($handler) {
43 1
			$handler->handlePush($event);
44 1
		};
45
46 1
		$this->router['tag_push'] = function (array $event) use($handler) {
47 1
			$handler->handleTag($event);
48 1
		};
49 4
	}
50
51
52 4
	public function __invoke(Request $request, Response $response, $args)
53
	{
54 4
		$body = $request->getParsedBody();
55 4
		if ( ! isset($body['object_kind'])) {
56 1
			return $response->withStatus(500);
57
		}
58
59 3
		foreach ($request->getHeader('X-Gitlab-Token') as $secret) {
60 3
			if ($secret === $this->secret) {
61 3
				$this->secured = TRUE;
62 3
			}
63 3
		}
64
65 3
		if ($this->secret !== NULL && ! $this->secured) {
66
			return $response->withStatus(403);
67
		}
68
69 3
		if (isset($this->router[$body['object_kind']])) {
70 3
			$this->router[$body['object_kind']]($body);
71 3
		}
72
73
74 3
		return $response->withStatus(200);
75
	}
76
77
}
78