HookResolver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 46
ccs 14
cts 14
cp 1
rs 10
c 2
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 14 3
A objectValidation() 0 14 3
1
<?php
2
3
namespace G2R\Gitlab;
4
5
use G2R\Exception\Exception;
6
use G2R\Gitlab\Hook\Build;
7
use G2R\Gitlab\Hook\Push;
8
use G2R\Gitlab\Hook\Tag;
9
10
class HookResolver
11
{
12
    /**
13
     * Load a gitlab or gitlab CI webhook handler.
14
     *
15
     * @param string $hookContent
16
     *
17
     * @return GitlabCiHook|GitlabHook
18
     */
19 10
    public static function load($hookContent)
20
    {
21 10
        $hookContent = self::objectValidation($hookContent);
22
23 6
        if ($hookContent->object_kind === 'push') {
24 2
            return new Push($hookContent);
25
        }
26
27 4
        if ($hookContent->tag) {
28 2
            return new Tag($hookContent);
29
        }
30
31 2
        return new Build($hookContent);
32
    }
33
34
    /**
35
     * Check if the hook content has a valid object kind.
36
     *
37
     * @param string $hookContent
38
     *
39
     * @return string $hookContent
40
     */
41 10
    private static function objectValidation($hookContent)
42
    {
43 10
        $hookContent = json_decode($hookContent);
44
45 10
        if (!isset($hookContent->object_kind)) {
46 2
            throw new Exception('Object kind not found in the hook data');
47
        }
48
49 8
        if (!in_array($hookContent->object_kind, ['push', 'build'])) {
50 2
            throw new Exception('Unknown Object kind from the hook');
51
        }
52
53 6
        return $hookContent;
54
    }
55
}
56