Passed
Push — master ( dde30a...f17d1c )
by Jan
02:49
created

BashRestController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 85
ccs 43
cts 43
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B __invoke() 0 28 6
B flatten() 0 25 5
1
<?php
2
3
/**
4
 * This file is part of Lekarna.cz (http://www.lekarna.cz/)
5
 *
6
 * Copyright (c) 2014 Pears Health Cyber, s.r.o. (http://pearshealthcyber.cz)
7
 *
8
 * For the full copyright and license information, please view
9
 * the file LICENSE that was distributed with this source code.
10
 */
11
12
namespace App;
13
14
use Interop\Container\ContainerInterface;
15
use Slim\Http\Request;
16
use Slim\Http\Response;
17
18
19
final class BashRestController
20
{
21
22
	const SECRET_HEADER = 'X-Secret';
23
24
	/**
25
	 * @var Executor
26
	 */
27
	private $executor;
28
29
	/**
30
	 * @var string
31
	 */
32
	private $secret;
33
34
	/**
35
	 * @var array
36
	 */
37
	private $scripts;
38
39 5
	public function __construct(ContainerInterface $ci, Executor $executor)
40
	{
41 5
		$this->executor = $executor;
42 5
		$this->secret = $ci->get('settings')['secret'];
43 5
		$this->scripts = $ci->get('bashREST');
0 ignored issues
show
Documentation Bug introduced by
It seems like $ci->get('bashREST') of type * is incompatible with the declared type array of property $scripts.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44 5
	}
45
46
47 5
	public function __invoke(Request $request, Response $response, array $args)
48
	{
49 5
		$secured = FALSE;
50 5
		foreach ($request->getHeader(self::SECRET_HEADER) as $secret) {
51 4
			if ($secret == $this->secret) { // allow cast
52 4
				$secured = TRUE;
53 4
			}
54 5
		}
55
56 5
		if ($this->secret !== NULL && ! $secured) {
57 1
			return $response->withStatus(403);
58
		}
59
60 4
		$projectName = $args['group'] . '/' . $args['project'];
61 4
		$action = $args['action'];
62
63 4
		if ( ! isset($this->scripts[$projectName][$action])) {
64 1
			return $response->withStatus(404);
65
		}
66
67 3
		return $response->withStatus(200)
68 3
			->withJson([
69 3
				'result' => $this->executor->executeCommand(
70 3
					$this->scripts[$projectName][$action],
71 3
					$this->flatten($request->getParsedBody())
0 ignored issues
show
Bug introduced by
It seems like $request->getParsedBody() targeting Slim\Http\Request::getParsedBody() can also be of type null or object; however, App\BashRestController::flatten() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
72 3
				)
73 3
			]);
74
	}
75
76
77 3
	private function flatten(array $data)
78
	{
79
		$toProcess = [[
80 3
			'data' => $data,
81
			'prefix' => 'HOOK'
82 3
		]];
83
84 3
		$flattened = [];
85
86 3
		while ( ! empty($toProcess)) {
87 3
			$actual = array_pop($toProcess);
88 3
			foreach ($actual['data'] as $key => $value) {
89 1
				if (is_scalar($value)) {
90 1
					$flattened[$actual['prefix'] . '_' . $key] = $value;
91 1
				} else if (is_array($value)) {
92 1
					array_push($toProcess, [
93 1
						'data' => $value,
94 1
						'prefix' => $actual['prefix'] . '_' . $key
95 1
					]);
96 1
				}
97 3
			}
98 3
		}
99
100 3
		return $flattened;
101
	}
102
103
}
104