Completed
Push — master ( e960f4...d2d510 )
by Randy
02:01
created

Hydrate::verifyLoggerPresence()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2.0625
1
<?php
2
3
namespace Dgame\Soap\Hydrator;
4
5
use Dgame\Object\ObjectFacade;
6
use Dgame\Soap\Element;
7
use Dgame\Variants\Variants;
8
use Monolog\Handler\NullHandler;
9
use Monolog\Handler\StreamHandler;
10
use Monolog\Logger;
11
use Monolog\Processor\PsrLogMessageProcessor;
12
use Monolog\Registry;
13
14
/**
15
 * Class Hydrate
16
 * @package Dgame\Soap\Hydrator
17
 */
18
final class Hydrate
19
{
20
    /**
21
     * @var ObjectFacade
22
     */
23
    private $facade;
24
    /**
25 9
     * @var Element
26
     */
27 9
    private $element;
28
29 9
    /**
30 9
     * Hydrate constructor.
31
     *
32
     * @param ClassMapper $mapper
33
     * @param Element     $element
34
     */
35
    public function __construct(ClassMapper $mapper, Element $element)
36
    {
37
        $object = $mapper->new($element->getName());
38 9
        if ($object !== null) {
39
            $this->facade = new ObjectFacade($object);
40 9
        }
41
42
        $this->element = $element;
43
44
        self::verifyLoggerPresence();
45
    }
46
47
    /**
48 5
     *
49
     */
50 5
    private static function verifyLoggerPresence()
51 5
    {
52 5
        if (!Registry::hasLogger(Hydrator::class)) {
53
            $log = new Logger(Hydrator::class);
54
            $log->pushHandler(new NullHandler());
55
            Registry::addLogger($log);
56
        }
57
    }
58
59
    /**
60
     * @return ObjectFacade
61
     */
62
    public function getFacade(): ObjectFacade
63
    {
64 8
        if (!$this->hasFacade()) {
65
            Registry::getInstance(Hydrator::class)->error('Invalid Hydrate in use');
66 8
        }
67 8
68
        return $this->facade;
69
    }
70 1
71
    /**
72
     * @return Element
73
     */
74
    public function getElement(): Element
75
    {
76 5
        return $this->element;
77
    }
78 5
79
    /**
80
     * @return bool
81
     */
82
    public function hasFacade(): bool
83
    {
84 5
        return $this->facade !== null;
85
    }
86 5
87
    /**
88
     * @param string $name
89
     * @param        $value
90
     */
91
    public function assign(string $name, $value)
92
    {
93
        if (!$this->getFacade()->setValue($name, $value)) {
94
            Registry::getInstance(Hydrator::class)->warning(
95
                'Could not assign value {value} of {name}',
96
                ['value' => var_export($value, true), 'name' => $name]
97
            );
98
        }
99
    }
100
101
    /**
102
     * @param Hydrate $hydrate
0 ignored issues
show
Documentation introduced by
Should the type for parameter $hydrate not be \self?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
103
     */
104
    public function append(self $hydrate)
105
    {
106
        $facade  = $hydrate->getFacade();
107
        $class   = $facade->getReflection()->getShortName();
108
        $element = $hydrate->getElement()->getName();
109
110
        foreach (Variants::ofArguments($class, $element)->withCamelSnakeCase() as $name) {
111
            if ($this->getFacade()->setValue($name, $facade->getObject())) {
112
                return;
113
            }
114
        }
115
116
        Registry::getInstance(Hydrator::class)->warning(
117
            'Could not append object {name}',
118
            ['name' => $element]
119
        );
120
    }
121
}