Completed
Push — master ( 1709fe...4c14c6 )
by Mathias
24s queued 16s
created

JobDescriptionHydrator::hydrate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 9.9
cc 2
nc 2
nop 2
crap 6
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright https://yawik.org/COPYRIGHT.php
7
 * @license   MIT
8
 * @author    [email protected]
9
 */
10
11
namespace Jobs\Form\Hydrator;
12
13
use Core\Entity\Hydrator\EntityHydrator;
14
15
// @TODO The JobDescriptionHydrator is used for every Form in the Description, this produces some overhead,
16
// @TODO correctly their should be one Hydrator for every Form
17
class JobDescriptionHydrator extends EntityHydrator
18
{
19
    /* (non-PHPdoc)
20
     * @see \Laminas\Hydrator\HydratorInterface::extract()
21
     */
22
    public function extract($object)
23
    {
24
        $data = parent::extract($object);
25
        if (!method_exists($object, 'getTemplateValues')) {
26
            return $data;
27
        }
28
29
        /** @var \Jobs\Entity\TemplateValues $values */
30
        $values = $object->getTemplateValues();
31
        $data['description-description']    = $values->getDescription();
32
        $data['description-requirements']   = $values->getRequirements();
33
        $data['description-benefits']       = $values->getBenefits();
34
        $data['description-qualifications'] = $values->getQualifications();
35
        $data['description-title']          = $values->getTitle();
36
        $data['description-html']           = $values->getHtml();
37
38
        return $data;
39
    }
40
41
    public function hydrate(array $data, $object)
42
    {
43
        $object = parent::hydrate($data, $object);
44
45
        if (!method_exists($object, 'getTemplateValues')) {
46
            return $object;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $object also could return the type array which is incompatible with the return type mandated by Laminas\Hydrator\HydrationInterface::hydrate() of object.
Loading history...
47
        }
48
49
        $values = $object->getTemplateValues();
50
51
        $this->hydrateTemplateValue('description', $data, $values);
52
        $this->hydrateTemplateValue('requirements', $data, $values);
53
        $this->hydrateTemplateValue('benefits', $data, $values);
54
        $this->hydrateTemplateValue('qualifications', $data, $values);
55
        $this->hydrateTemplateValue('title', $data, $values);
56
        $this->hydrateTemplateValue('html', $data, $values);
57
58
        return $object;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $object also could return the type object which is incompatible with the return type mandated by Laminas\Hydrator\HydrationInterface::hydrate() of object.
Loading history...
59
    }
60
61
    private function hydrateTemplateValue($name, $data, $object)
62
    {
63
        $key = "description-$name";
64
        $setter = "set$name";
65
66
        if (isset($data[$key])) {
67
            $object->$setter($data[$key]);
68
        }
69
    }
70
}
71