Passed
Push — master ( 602e7e...6494b6 )
by Jasper
01:43
created

SituationParser::getActivityType()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Swis\Melvin\Parsers;
6
7
use DateTime;
8
use stdClass;
9
use Swis\Melvin\Enums\ActivityType;
10
use Swis\Melvin\Enums\Delay;
11
use Swis\Melvin\Enums\EventType;
12
use Swis\Melvin\Enums\Impact;
13
use Swis\Melvin\Enums\PersonType;
14
use Swis\Melvin\Enums\RoadAuthorityType;
15
use Swis\Melvin\Enums\SituationStatus;
16
use Swis\Melvin\Enums\Source;
17
use Swis\Melvin\Enums\WorkObject;
18
use Swis\Melvin\Enums\WorkType;
19
use Swis\Melvin\Models\Location;
20
use Swis\Melvin\Models\Person;
21
use Swis\Melvin\Models\RoadAuthority;
22
use Swis\Melvin\Models\Situation;
23
24
class SituationParser
25
{
26
    protected GeometryParser $geometryParser;
27
28
    protected PeriodParser $periodParser;
29
30
    protected AttachmentParser $attachmentParser;
31
32
    protected RestrictionParser $restrictionParser;
33
34
    protected DetourParser $detourParser;
35
36
    public function __construct(
37
        GeometryParser $geometryParser,
38
        PeriodParser $periodParser,
39
        AttachmentParser $attachmentParser,
40
        RestrictionParser $restrictionParser,
41
        DetourParser $detourParser
42
    ) {
43
        $this->geometryParser = $geometryParser;
44
        $this->periodParser = $periodParser;
45
        $this->attachmentParser = $attachmentParser;
46
        $this->restrictionParser = $restrictionParser;
47
        $this->detourParser = $detourParser;
48
    }
49
50
    public function parse(stdClass $object, array $restrictions, array $detours = []): Situation
51
    {
52
        if ($roadAuthority = $object->properties->roadAuthority ?? null) {
53
            $roadAuthority = new RoadAuthority(
54
                $roadAuthority->id,
55
                RoadAuthorityType::from($roadAuthority->type),
56
                $roadAuthority->name
57
            );
58
        }
59
60
        if ($createdBy = $object->properties->createdBy ?? null) {
61
            $createdBy = new Person(
62
                $createdBy->firstName,
63
                $createdBy->prefix,
64
                $createdBy->lastName,
65
                PersonType::from($createdBy->type)
66
            );
67
        }
68
69
        if ($lastChangedBy = $object->properties->lastChangedBy ?? null) {
70
            $lastChangedBy = new Person(
71
                $lastChangedBy->firstName,
72
                $lastChangedBy->prefix,
73
                $lastChangedBy->lastName,
74
                PersonType::from($lastChangedBy->type)
75
            );
76
        }
77
78
        $location = new Location(
79
            $object->properties->location->city,
80
            $object->properties->location->road,
81
            $object->properties->location->district,
82
            $object->properties->location->comment ?? ''
83
        );
84
85
        return new Situation(
86
            $object->id,
87
            str_contains($object->properties->type, '_EXTERNAL'),
88
            $this->geometryParser->parse($object->geometry),
89
            $this->getName($object),
90
            $this->getActivityType($object),
91
            ($object->properties->workObject ?? '') ? WorkObject::from($object->properties->workObject) : null,
92
            $object->properties->impact !== 'EMPTY' ? Impact::from($object->properties->impact) : null,
93
            $object->properties->project,
94
            Source::from($object->properties->source),
95
            $object->properties->published,
96
            $object->properties->url ?: null,
97
            ($object->properties->urlDescription ?? '') ?: null,
98
            Delay::from($object->properties->delay),
99
            ($object->properties->workType ?? '') ? WorkType::from($object->properties->workType) : null,
100
            ($object->properties->eventType ?? '') ? EventType::from($object->properties->eventType) : null,
101
            ($object->properties->eventName ?? '') ?: null,
102
            ($object->properties->addition ?? '') ?: null,
103
            SituationStatus::from($object->properties->status),
104
            $roadAuthority,
105
            $location,
106
            array_map([$this->periodParser, 'parse'], $object->properties->periods, array_keys($object->properties->periods)),
107
            ($object->properties->createdAt ?? null) ? new DateTime($object->properties->createdAt) : null,
108
            $createdBy,
109
            $lastChangedBy,
110
            array_map([$this->attachmentParser, 'parse'], $object->properties->attachments ?? [], array_keys($object->properties->attachments ?? [])),
111
            array_map([$this->restrictionParser, 'parse'], $restrictions, array_keys($restrictions)),
112
            array_map([$this->detourParser, 'parse'], $detours, array_keys($detours))
113
        );
114
    }
115
116
    protected function getName(stdClass $object): string
117
    {
118
        if (property_exists($object->properties, 'name')) {
119
            return $object->properties->name;
120
        }
121
122
        $eventName = ($object->properties->eventName ?? '') ?: null;
123
        $eventType = ($object->properties->eventType ?? '') ? EventType::from($object->properties->eventType) : null;
124
        $workType = ($object->properties->workType ?? '') ? WorkType::from($object->properties->workType) : null;
125
        $workObject = ($object->properties->workObject ?? '') ? WorkObject::from($object->properties->workObject) : null;
126
127
        $description = '';
128
        if ($eventName) {
129
            $description = $eventName;
130
        } elseif ($eventType) {
131
            $description = $eventType->getLabel();
132
        } elseif ($workType) {
133
            $description = $workType->getLabel();
134
135
            if ($workObject) {
136
                $description = str_replace('...', $workObject->getLabel(), $description);
137
            }
138
        }
139
140
        return trim(
141
            preg_replace(
142
                '/\s+/',
143
                ' ',
144
                sprintf(
145
                    '%s %s %s %s %s',
146
                    $object->properties->location->road,
147
                    $object->properties->location->district,
148
                    $object->properties->location->city,
149
                    $description,
150
                    $object->properties->addition ?? ''
151
                )
152
            )
153
        );
154
    }
155
156
    protected function getActivityType(stdClass $object): ActivityType
157
    {
158
        $eventType = ($object->properties->eventType ?? '') ? EventType::from($object->properties->eventType) : null;
159
160
        if ($eventType) {
161
            return ActivityType::EVENT();
162
        }
163
164
        return ActivityType::WORK();
165
    }
166
}
167