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.

HydratorDecoratorFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

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