Completed
Push — master ( 0913ed...15a2b1 )
by Mathias
21s queued 15s
created

ApiApplicationHydrator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 59
c 1
b 0
f 0
dl 0
loc 85
ccs 0
cts 58
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setServerUrl() 0 3 1
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
    private $serverUrl;
29
30
    public function setServerUrl(string $serverUrl)
31
    {
32
        $this->serverUrl = $serverUrl;
33
    }
34
35
    public function hydrate(array $data, $object)
36
    {
37
        $applicationData = [];
38
39
        foreach ($data as $key => $value) {
40
            switch ($key) {
41
                default:
42
                    $applicationData[$key] = $value;
43
                    break;
44
45
                case "contact":
46
                    $contact = $object->getContact();
47
                    if (isset($value['birthday'])) {
48
                        [$year, $month, $day] = explode('-', $value['birthday']);
49
                        $value['birth_year'] = $year;
50
                        $value['birth_month'] = $month;
51
                        $value['birth_day'] = $day;
52
                    }
53
                    $hydrator = new ClassMethods();
54
                    $hydrator->addStrategy('image', new FileUploadStrategy(new Attachment()));
55
                    $hydrator->hydrate($value, $contact);
56
                    break;
57
58
                case "attachments":
59
                    $collection = $object->getAttachments();
60
                    $strategy = new FileUploadStrategy(new Attachment());
61
                    foreach ($value as $uploadedFileData) {
62
                        $file = $strategy->hydrate($uploadedFileData);
63
                        $collection->add($file);
64
                    }
65
                    break;
66
67
                case "facts":
68
                    $embedded = $object->{"get$key"}();
69
                    parent::hydrate($value, $embedded);
70
                    break;
71
            }
72
        }
73
74
        parent::hydrate($applicationData, $object);
75
76
        return $object;
77
    }
78
79
    public function extract($object)
80
    {
81
        $data = parent::extract($object);
82
        $data['job'] = $object->getJob()->getApplyId();
83
        $data['user'] = $object->getUser() ? $object->getUser()->getId() : null;
84
        $data['contact'] = parent::extract($object->getContact());
85
        $data['contact']['image'] =
86
            $object->getContact()->getImage()
87
            ? $this->serverUrl . $object->getContact()->getImage()->getUri()
88
            : null
89
        ;
90
        $data['attachments'] = [];
91
        foreach ($object->getAttachments() as $file) {
92
           $data['attachments'][] = $this->serverUrl . $file->getUri();
93
        }
94
95
        unset(
96
            $data['is_draft'],
97
            $data['history'],
98
            $data['read_by'],
99
            $data['subscriber'],
100
            $data['permissions'],
101
            $data['refs'],
102
            $data['searchable_properties'],
103
            $data['keywords'],
104
            $data['comments'],
105
            $data['comments_message'],
106
            $data['rating'],
107
            $data['attributes'],
108
            $data['cv']
109
        );
110
        return $data;
111
    }
112
}
113