Completed
Push — develop ( c185af...7af18d )
by
unknown
11:25
created

TemplateValuesHydrator::extract()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license    MIT
7
 * @copyright  2013 - 2015 Cross Solution <http://cross-solution.de>
8
 */
9
10
/** */
11
namespace Jobs\Entity\Hydrator;
12
13
use Core\Entity\Hydrator\EntityHydrator;
14
use Jobs\Entity\TemplateValues;
15
16
17
/**
18
 * Hydrator handles {@link \Jobs\Entity\TemplateValues}.
19
 *
20
 * Additionally hydrates and extracts the FreeValues keys it is configured to care for.
21
 *
22
 * @author Mathias Gelhausen <[email protected]>
23
 * @since  0.23
24
 */
25
class TemplateValuesHydrator extends EntityHydrator
26
{
27
    /**
28
     * The FreeValues keys to care about.
29
     *
30
     * @var array
31
     */
32
    protected $freeValuesKeys = [];
33
34
    /**
35
     * Creates an instance.
36
     *
37
     * @param array|null $freeValuesKeys
38
     */
39
    public function __construct($freeValuesKeys = null)
40
    {
41
        parent::__construct();
42
43
        if ($freeValuesKeys) {
44
            $this->setFreeValuesKeys($freeValuesKeys);
45
        }
46
    }
47
48
    /**
49
     * Gets the FreeValues keys this instance cares about.
50
     *
51
     * @return array
52
     */
53
    public function getFreeValuesKeys()
54
    {
55
        return $this->freeValuesKeys;
56
    }
57
58
    /**
59
     * Sets the FreeValues keys to care about.
60
     *
61
     * @param array $freeValuesKeys
62
     *
63
     * @return self
64
     */
65
    public function setFreeValuesKeys(array $freeValuesKeys)
66
    {
67
        $this->freeValuesKeys = $freeValuesKeys;
68
69
        return $this;
70
    }
71
72
    /**
73
     * Extract values (including the configured FreeValues) from an TemplateValues entity.
74
     *
75
     * @param TemplateValues $object
76
     *
77
     * @return array
78
     */
79
    public function extract($object)
80
    {
81
        $data = parent::extract($object);
82
83
        foreach ($this->getFreeValuesKeys() as $key) {
84
            $data[$key] = $object->get($key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data[$key] is correct as $object->get($key) (which targets Jobs\Entity\TemplateValues::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
        }
86
87
        return $data;
88
    }
89
90
    /**
91
     * Hydrate a TemplateValues entity with the provided $data including the configured FreeValues.
92
     *
93
     * @param  array          $data
94
     * @param  TemplateValues $object
95
     *
96
     * @return TemplateValues
97
     */
98
    public function hydrate(array $data, $object)
99
    {
100
        parent::hydrate($data, $object);
101
102
        foreach ($this->getFreeValuesKeys() as $key) {
103
            if (isset($data[$key])) {
104
                $object->set($key, $data[$key]);
105
            }
106
        }
107
108
        return $object;
109
    }
110
}