Passed
Push — master ( 7eb6b7...1868e2 )
by Jasper
05:04 queued 03:13
created

SituationParser::getActivityType()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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