Passed
Push — master ( 4fcf6f...e67987 )
by Jan
03:06
created

BashRestController::isSecured()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 6
nc 6
nop 1
crap 4
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 InvalidArgumentException;
16
use Slim\Http\Request;
17
use Slim\Http\Response;
18
19
20
final class BashRestController
21
{
22
23
	use SecuredTrait;
24
25
	const SECRET_HEADER = 'X-Secret';
26
27
	/**
28
	 * @var Executor
29
	 */
30
	private $executor;
31
32
	/**
33
	 * @var string
34
	 */
35
	private $secret;
36
37
	/**
38
	 * @var array
39
	 */
40
	private $scripts;
41
42 6
	public function __construct(ContainerInterface $ci, Executor $executor)
43
	{
44 6
		$this->executor = $executor;
45 6
		$this->secret = (string) $ci->get('settings')['secret'];
46 6
		$this->scripts = (array) $ci->get('bashREST');
47 6
	}
48
49
50
	/**
51
	 * @param Request $request
52
	 * @param Response $response
53
	 * @param array $args
54
	 * @return Response
55
	 */
56 6
	public function __invoke(Request $request, Response $response, array $args)
57
	{
58 6
		if ( ! $this->isSecured($request)) {
59 1
			return $response->withStatus(403);
60
		}
61
62 5
		$projectName = $args['group'] . '/' . $args['project'];
63 5
		$action = $args['action'];
64
65 5
		if ( ! $this->isHandled($projectName, $action)) {
66 1
			return $response->withStatus(404);
67
		}
68
69 4
		return $response->withStatus(200)
70 4
			->withJson($this->handle($request, $projectName, $action));
71
	}
72
73
74
	/**
75
	 * @param array|NULL $data
76
	 * @return array
77
	 */
78 4
	private function flatten($data)
79
	{
80 4
		if ($data === NULL) {
81 1
			return [];
82
		}
83
84 3
		if (is_object($data)) {
85 1
			throw new InvalidArgumentException('Unexpected parser result.');
86
		}
87
88
		$toProcess = [[
89 2
			'data' => $data,
90
			'prefix' => 'HOOK'
91 2
		]];
92
93 2
		$flattened = [];
94
95 2
		while ( ! empty($toProcess)) {
96 2
			$actual = array_pop($toProcess);
97 2
			$this->flattenProcessArray($actual, $flattened, $toProcess);
98 2
		}
99
100 2
		return $flattened;
101
	}
102
103
104
	/**
105
	 * @param string $projectName
106
	 * @param string $action
107
	 * @return bool
108
	 */
109 5
	private function isHandled($projectName, $action)
110
	{
111 5
		return isset($this->scripts[$projectName][$action]);
112
	}
113
114
115
	/**
116
	 * @param array $actual
117
	 * @param array $flattened
118
	 * @param array $toProcess
119
	 * @return array
120
	 */
121 2
	private function flattenProcessArray(array $actual, array &$flattened, array &$toProcess)
122
	{
123 2
		foreach ($actual['data'] as $key => $value) {
124 1
			if (is_scalar($value)) {
125 1
				$flattened[$actual['prefix'] . '_' . $key] = $value;
126 1
			} else {
127 1
				if (is_array($value)) {
128 1
					array_push(
129 1
						$toProcess,
130
						[
131 1
							'data' => $value,
132 1
							'prefix' => $actual['prefix'] . '_' . $key
133 1
						]
134 1
					);
135 1
				}
136
			}
137 2
		}
138 2
	}
139
140
141
	/**
142
	 * @param Request $request
143
	 * @param string $projectName
144
	 * @param string $action
145
	 * @return array
146
	 */
147 4
	private function handle(Request $request, $projectName, $action)
148
	{
149
		return [
150 4
			'result' => $this->executor->executeCommand(
151 4
				$this->scripts[$projectName][$action],
152 4
				$this->flatten($request->getParsedBody())
153 3
			)
154 3
		];
155
	}
156
157
}
158