Completed
Push — master ( 4706ec...aaa0db )
by Rémi
12:54
created

HookResolver::objectValidation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 3
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 = static::objectValidation($data);
0 ignored issues
show
Bug introduced by
Since objectValidation() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of objectValidation() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
20
21
        if ($data->object_kind === 'push') {
22
            return new Push($data);
23
        }
24
25
        if ($data->tag) {
26
            return new Tag($data);
27
        }
28
29
        return new Build($data);
30
    }
31
32
    /**
33
     * Check if the hook content has a valid object kind
34
     */
35
    private static function objectValidation($data)
36
    {
37
        $data = json_decode($data);
38
39
        if (!isset($data->object_kind)) {
40
            throw new Exception('Object kind not found in the hook data');
41
        }
42
43
        if (!in_array($data->object_kind, ['push', 'build'])) {
44
            throw new Exception('Unknown Object kind from the hook');
45
        }
46
47
        return $data;
48
    }
49
}
50