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

Handler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace App;
4
5
use Interop\Container\ContainerInterface;
6
7
8
class Handler
9
{
10
11
	/**
12
	 * @var array
13
	 */
14
	private $scripts;
15
16
	/**
17
	 * @var Executor
18
	 */
19
	private $executor;
20
21
22 4
	public function __construct(ContainerInterface $ci, Executor $executor)
23
	{
24 4
		$this->executor = $executor;
25 4
		$this->scripts = $ci->get('scripts');
0 ignored issues
show
Documentation Bug introduced by
It seems like $ci->get('scripts') 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...
26 4
	}
27
28
29 1
	public function handleDeploy(array $event, array $build)
30
	{
31 1
		if ($build['status'] === 'created') {
32 1
			$projectName = $event['project']['path_with_namespace'];
33 1
			$buildId = $build['id'];
34 1
			$commitId = $event['commit']['id'];
35 1
			$name = $build['name'];
36 1
			if (isset($this->scripts[$projectName]['deploy'][$name])) {
37 1
				$this->executeCommand($this->scripts[$projectName]['deploy'][$name], [
38 1
					'HOOK_PROJECT_PATH' => $projectName,
39 1
					'HOOK_ENV_NAME' => $name,
40 1
					'HOOK_BUILD_ID' => $buildId,
41
					'HOOK_BUILD_REF' => $commitId
42 1
				]);
43 1
			}
44 1
		}
45 1
	}
46
47
48 1 View Code Duplication
	public function handlePush(array $event)
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...
49
	{
50 1
		$projectName = $event['project']['path_with_namespace'];
51 1
		$ref = $event['ref'];
52 1
		if (isset($this->scripts[$projectName]['push'][$ref])) {
53 1
			$this->executeCommand($this->scripts[$projectName]['push'][$ref], [
54 1
				'HOOK_PROJECT_PATH' => $projectName,
55 1
				'HOOK_REF' => $ref,
56 1
				'HOOK_BRANCH'=> $this->extractBranchName($ref),
57 1
				'HOOK_BUILD_REF' => $event['after']
58 1
			]);
59 1
		}
60 1
	}
61
62
63 1 View Code Duplication
	public function handleTag(array $event)
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...
64
	{
65 1
		$projectName = $event['project']['path_with_namespace'];
66 1
		$ref = $event['ref'];
67 1
		if (isset($this->scripts[$projectName]['tag'])) {
68 1
			$this->executeCommand($this->scripts[$projectName]['tag'], [
69 1
				'HOOK_PROJECT_PATH' => $projectName,
70 1
				'HOOK_REF' => $ref,
71 1
				'HOOK_TAG'=> $this->extractTagName($ref),
72 1
				'HOOK_BUILD_REF' => $event['after']
73 1
			]);
74 1
		}
75 1
	}
76
77
78 1
	private function extractBranchName($ref)
79
	{
80 1
		return substr($ref, strlen('refs/heads/'));
81
	}
82
83
84 1
	private function extractTagName($ref)
85
	{
86 1
		return substr($ref, strlen('refs/tags/'));
87
	}
88
89
90
	/**
91
	 * @param string|array $scriptPath
92
	 * @param array $env
93
	 */
94 3
	private function executeCommand($scriptPath, array $env)
95
	{
96 3
		$this->executor->executeCommand($scriptPath, $env);
97 3
	}
98
99
}
100