GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b88bc4...2341f0 )
by Benjamin
02:09
created

HydratorDecoratorFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 98
ccs 28
cts 29
cp 0.9655
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getHydratorForClass() 0 11 4
A createHydratorForClass() 0 16 4
A createHydratorFile() 0 57 2
A getShortClassName() 0 4 1
1
<?php
2
3
namespace Lib\DB\ORM;
4
5
use GuzzleHttp\Psr7\UriResolver;
6
7
class HydratorDecoratorFactory
8
{
9
    protected $decorators = [];
10
11 3
    public function getShortClassName($fqcn)
12
    {
13 3
        $classArray = explode('\\', $fqcn);
14 3
        return array_pop($classArray);
15
    }
16
17 2
    public function getHydratorForClass($classOrObject, array $args = [])
18
    {
19 2
        if (!is_string($classOrObject) && is_object($classOrObject)) {
20 1
            $classOrObject = get_class($classOrObject);
21
        }
22
23 2
        if (!isset($this->decorators[$classOrObject])) {
24 2
            $this->createHydratorForClass($classOrObject, $args);
25
        }
26
27 2
        return $this->decorators[$this->getShortClassName($classOrObject)];
28
    }
29
30 3
    public function createHydratorForClass($class, array $args = [])
31
    {
32 3
        $shortClass    = $this->getShortClassName($class);
33 3
        $hydratorClass = $shortClass.'HydratorDecorator';
34 3
        $dir           = UriResolver::removeDotSegments(__DIR__.'/../../../../var/cache/orm/hydrators/');
35 3
        $hydratorFile  = $dir.$hydratorClass.'.php';
36
37 3
        if (!file_exists($hydratorFile) || !is_readable($hydratorFile)) {
38 1
            $this->createHydratorFile($class, $hydratorClass, $hydratorFile);
39
        }
40
41 3
        if (!class_exists($hydratorClass)) {
42
            include($hydratorFile);
43
        }
44
45 3
        $this->decorators[$shortClass] = new $hydratorClass(...$args);
46 3
    }
47
48 2
    public function createHydratorFile($entityClass, $hydratorClass, $hydratorFile)
49
    {
50 2
        $dir = UriResolver::removeDotSegments(dirname($hydratorFile));
51 2
        if (!is_dir($dir)) {
52 2
            mkdir($dir, 0777, true);
53
        }
54
55
        $classHeading = <<<EOD
56
<?php
57
58
use Lib\\Access\\Accessor;
59
60 2
class $hydratorClass extends $entityClass
61
EOD;
62
        $classBody = <<<'EOD'
63 2
{
64
    protected $accessor;
65
66
    public function hydrate(array $data = [])
67
    {
68
        $props = get_object_vars($this);
69
        $camelized = $this->camelizeData($data);
70
71
        foreach (array_keys($props) as $property) {
72
            if ('accessor' === $property) {
73
                continue;
74
            }
75
            if (array_key_exists($property, $camelized)) {
76
                $this->$property = $camelized[$property];
77
            }
78
        }
79
    }
80
81
    public function camelizeData(array $data)
82
    {
83
        $camelized = [];
84
        foreach ($data as $key => $value) {
85
            $camelized[$this->camelize($key)] = $value;
86
        }
87
        return $camelized;
88
    }
89
90
    public function camelize(string $string)
91
    {
92
        if (!$this->accessor || !($this->accessor instanceof Accessor)) {
93
            $this->accessor = new Accessor();
94
        }
95
        return $this->accessor->camelize($string);
96
    }
97
}
98
99
EOD;
100
        $class = <<<EOD
101 2
$classHeading
102 2
$classBody
103
EOD;
104 2
        return file_put_contents($hydratorFile, $class);
105
    }
106
}
107