Completed
Push — master ( a4d419...4706ec )
by Rémi
15:41
created

HookResolver::load()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 8.6737
cc 5
eloc 11
nc 5
nop 1
1
<?php namespace G2R\Gitlab;
2
3
use G2R\Exception\Exception;
4
use G2R\Gitlab\Hook\Build;
5
use G2R\Gitlab\Hook\Push;
6
use G2R\Gitlab\Hook\Tag;
7
8
class HookResolver
9
{
10
    /**
11
     * Load a gitlab or gitlab CI webhook handler
12
     *
13
     * @param $data
14
     *
15
     * @return GitlabCiHook|GitlabHook
16
     */
17
    public static function load($data)
18
    {
19
        $data = json_decode($data);
20
21
        if (!isset($data->object_kind)) {
22
            throw new Exception('Object kind not found in the hook data');
23
        }
24
25
        if (!in_array($data->object_kind, ['push', 'build'])) {
26
            throw new Exception('Unknown Object kind from the hook');
27
        }
28
29
        if ($data->object_kind === 'push') {
30
            return new Push($data);
31
        }
32
33
        if ($data->tag) {
34
            return new Tag($data);
35
        }
36
37
        return new Build($data);
38
    }
39
}
40