HookHandler   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 99
ccs 49
cts 49
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handleDeploy() 0 17 3
A handlePush() 0 13 2
A handleTag() 0 12 2
A extractBranchName() 0 4 1
A extractTagName() 0 4 1
A executeCommand() 0 4 1
1
<?php
2
3
namespace App;
4
5
use Interop\Container\ContainerInterface;
6
7
8
class HookHandler
9
{
10
11
	/**
12
	 * @var array
13
	 */
14
	private $scripts;
15
16
	/**
17
	 * @var Executor
18
	 */
19
	private $executor;
20
21
22 8
	public function __construct(ContainerInterface $ci, Executor $executor)
23
	{
24 8
		$this->executor = $executor;
25 8
		$this->scripts = (array) $ci->get('scripts');
26 8
	}
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
	public function handlePush(array $event)
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 3
	public function handleTag(array $event)
64
	{
65 3
		$projectName = $event['project']['path_with_namespace'];
66 3
		if (isset($this->scripts[$projectName]['tag'])) {
67 3
			$this->executeCommand($this->scripts[$projectName]['tag'], [
68 3
				'HOOK_PROJECT_PATH' => $projectName,
69 3
				'HOOK_REF' => $event['ref'],
70 3
				'HOOK_TAG'=> $this->extractTagName($event['ref']),
71 3
				'HOOK_BUILD_REF' => $event['after']
72 3
			]);
73 3
		}
74 3
	}
75
76
77
	/**
78
	 * @param string $ref
79
	 * @return string
80
	 */
81 1
	private function extractBranchName($ref)
82
	{
83 1
		return substr($ref, strlen('refs/heads/'));
84
	}
85
86
87
	/**
88
	 * @param string $ref
89
	 * @return string
90
	 */
91 3
	private function extractTagName($ref)
92
	{
93 3
		return substr($ref, strlen('refs/tags/'));
94
	}
95
96
97
	/**
98
	 * @param string|array $scriptPath
99
	 * @param array $env
100
	 */
101 5
	private function executeCommand($scriptPath, array $env)
102
	{
103 5
		$this->executor->executeCommand($scriptPath, $env);
104 5
	}
105
106
}
107