Passed
Push — master ( 4cb164...4fcf6f )
by Jan
03:04
created

HookController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 110
Duplicated Lines 10 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
c 2
b 0
f 0
lcom 2
cbo 4
dl 11
loc 110
ccs 43
cts 43
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 3
A __invoke() 0 17 4
A isValid() 0 9 2
A isSecured() 11 11 4
A isHandled() 0 5 1
A handle() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
	const SECRET_HEADER = 'X-Gitlab-Token';
14
15
	/**
16
	 * @var string
17
	 */
18
	private $secret;
19
20
	/**
21
	 * @var callable[]
22
	 */
23
	private $router;
24
25
26 8
	public function __construct(ContainerInterface $ci, HookHandler $handler)
27
	{
28 8
		$this->secret = (string) $ci->get('settings')['secret'];
29
30
		$this->router['pipeline'] = function (array $event) use($handler) {
31 1
			foreach ($event['builds'] as $build) {
32 1
				if ($build['stage'] === 'deploy') {
33 1
					$handler->handleDeploy($event, $build);
34 1
				}
35 1
			}
36 1
		};
37
38
		$this->router['push'] = function (array $event) use($handler) {
39 1
			$handler->handlePush($event);
40 1
		};
41
42 3
		$this->router['tag_push'] = function (array $event) use($handler) {
43 3
			$handler->handleTag($event);
44 3
		};
45 8
	}
46
47
48 8
	public function __invoke(Request $request, Response $response, array $args)
49
	{
50 8
		if ( ! $this->isValid($request)) {
51 1
			return $response->withStatus(500);
52
		}
53
54 7
		if ( ! $this->isSecured($request)) {
55 1
			return $response->withStatus(403);
56
		}
57
58 6
		if ($this->isHandled($request)) {
59 5
			$this->handle($request);
60 5
			return $response->withStatus(200);
61
		}
62
63 1
		return $response->withStatus(404);
64
	}
65
66
67
	/**
68
	 * @param Request $request
69
	 * @return bool
70
	 */
71 8
	private function isValid(Request $request)
72
	{
73 8
		$body = $request->getParsedBody();
74 8
		if ( ! isset($body['object_kind'])) {
75 1
			return FALSE;
76
		}
77
78 7
		return TRUE;
79
	}
80
81
82
	/**
83
	 * @param Request $request
84
	 * @return bool
85
	 */
86 7 View Code Duplication
	private function isSecured(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
	{
88 7
		$secured = FALSE;
89 7
		foreach ($request->getHeader(self::SECRET_HEADER) as $secret) {
90 6
			if ($secret == $this->secret) { // allow cast
91 6
				$secured = TRUE;
92 6
			}
93 7
		}
94
95 7
		return $this->secret === NULL || $secured;
96
	}
97
98
99
	/**
100
	 * @param Request $request
101
	 * @return bool
102
	 */
103 6
	private function isHandled(Request $request)
104
	{
105 6
		$body = $request->getParsedBody();
106 6
		return isset($this->router[$body['object_kind']]);
107
	}
108
109
110
	/**
111
	 * @param Request $request
112
	 */
113 5
	private function handle(Request $request)
114
	{
115 5
		$body = $request->getParsedBody();
116 5
		$this->router[$body['object_kind']]($body);
117 5
	}
118
119
}
120