Completed
Pull Request — master (#588)
by Mathias
07:35
created

ApiApplicationHydrator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 57
c 1
b 0
f 0
dl 0
loc 79
ccs 0
cts 56
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extract() 0 32 4
B hydrate() 0 42 7
1
<?php
2
3
/**
4
 * YAWIK
5
 *
6
 * @filesource
7
 * @copyright 2020 CROSS Solution <https://www.cross-solution.de>
8
 * @license MIT
9
 */
10
11
declare(strict_types=1);
12
13
namespace Applications\Entity\Hydrator;
14
15
use Applications\Entity\Attachment;
16
use Core\Entity\Hydrator\EntityHydrator;
17
use Core\Entity\Hydrator\Strategy\FileUploadStrategy;
18
use Laminas\Hydrator\ClassMethods;
19
20
/**
21
 * TODO: description
22
 *
23
 * @author Mathias Gelhausen <[email protected]>
24
 * TODO: write tests
25
 */
26
class ApiApplicationHydrator extends ClassMethods
27
{
28
29
    public function hydrate(array $data, $object)
30
    {
31
        $applicationData = [];
32
33
        foreach ($data as $key => $value) {
34
            switch ($key) {
35
                default:
36
                    $applicationData[$key] = $value;
37
                    break;
38
39
                case "contact":
40
                    $contact = $object->getContact();
41
                    if (isset($value['birthday'])) {
42
                        [$year, $month, $day] = explode('-', $value['birthday']);
43
                        $value['birth_year'] = $year;
44
                        $value['birth_month'] = $month;
45
                        $value['birth_day'] = $day;
46
                    }
47
                    $hydrator = new ClassMethods();
48
                    $hydrator->addStrategy('image', new FileUploadStrategy(new Attachment()));
49
                    $hydrator->hydrate($value, $contact);
50
                    break;
51
52
                case "attachments":
53
                    $collection = $object->getAttachments();
54
                    $strategy = new FileUploadStrategy(new Attachment());
55
                    foreach ($value as $uploadedFileData) {
56
                        $file = $strategy->hydrate($uploadedFileData);
57
                        $collection->add($file);
58
                    }
59
                    break;
60
61
                case "facts":
62
                    $embedded = $object->{"get$key"}();
63
                    parent::hydrate($value, $embedded);
64
                    break;
65
            }
66
        }
67
68
        parent::hydrate($applicationData, $object);
69
70
        return $object;
71
    }
72
73
    public function extract($object)
74
    {
75
        $data = parent::extract($object);
76
        $data['job'] = $object->getJob()->getApplyId();
77
        $data['user'] = $object->getUser() ? $object->getUser()->getId() : null;
78
        $data['contact'] = parent::extract($object->getContact());
79
        $data['contact']['image'] =
80
            $object->getContact()->getImage()
81
            ? $object->getContact()->getImage()->getUri()
82
            : null
83
        ;
84
        $data['attachments'] = [];
85
        foreach ($object->getAttachments() as $file) {
86
           $data['attachments'][] = $file->getUri();
87
        }
88
89
        unset(
90
            $data['is_draft'],
91
            $data['history'],
92
            $data['read_by'],
93
            $data['subscriber'],
94
            $data['permissions'],
95
            $data['refs'],
96
            $data['searchable_properties'],
97
            $data['keywords'],
98
            $data['comments'],
99
            $data['comments_message'],
100
            $data['rating'],
101
            $data['attributes'],
102
            $data['cv']
103
        );
104
        return $data;
105
    }
106
}
107