HookResolver::objectValidation()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 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