RestrictionParser::parse()   B
last analyzed

Complexity

Conditions 11
Paths 16

Size

Total Lines 46
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 46
ccs 36
cts 36
cp 1
rs 7.3166
cc 11
nc 16
nop 2
crap 11

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Swis\Melvin\Parsers;
6
7
use Swis\Melvin\Enums\Direction;
8
use Swis\Melvin\Enums\RestrictionType;
9
use Swis\Melvin\Enums\RoadManagementType;
10
use Swis\Melvin\Enums\TransportMode;
11
use Swis\Melvin\Enums\VehicleType;
12
use Swis\Melvin\Models\LaneInformation;
13
use Swis\Melvin\Models\Restriction;
14
use Swis\Melvin\Models\RestrictionExtraInfo;
15
use Swis\Melvin\Models\VehicleInformation;
16
17
class RestrictionParser
18
{
19 6
    public function __construct(protected GeometryParser $geometryParser)
20
    {
21 6
    }
22
23 6
    public function parse(\stdClass $object, int $index): Restriction
24
    {
25 6
        if ($emergencyServices = $object->properties->emergencyServices ?? $object->properties->emergency ?? null) {
26 6
            $emergencyServices = $this->parseExtraInfo($emergencyServices);
27
        }
28
29 6
        if ($publicTransport = $object->properties->publicTransport ?? null) {
30 6
            $publicTransport = $this->parseExtraInfo($publicTransport);
31
        }
32
33 6
        if ($laneInformation = $object->properties->laneInformation ?? null) {
34 6
            $laneInformation = new LaneInformation(
35 6
                $laneInformation->numberOfLanesRestricted,
36 6
                $laneInformation->numberOfOperationalLanes,
37 6
                $laneInformation->originalNumberOfLanes
38 3
            );
39
        }
40
41
        // N.B. vehicleInformation is often an empty object when unknown
42 6
        if ($vehicleInformation = (array) ($object->properties->vehicleInformation ?? []) ? $object->properties->vehicleInformation : null) {
43 6
            $vehicleInformation = new VehicleInformation(
44 6
                $vehicleInformation->heightCharacteristic ?? null,
45 6
                $vehicleInformation->lengthCharacteristic ?? null,
46 6
                $vehicleInformation->widthCharacteristic ?? null,
47 6
                $vehicleInformation->grossWeightCharacteristic ?? null,
48 3
            );
49
        }
50
51 6
        return new Restriction(
52 6
            $object->id,
53 6
            str_contains($object->properties->type, '_EXTERNAL'),
54 6
            $object->geometry ? $this->geometryParser->parse($object->geometry) : null,
55 6
            ($object->properties->name ?? '') ?: sprintf('Beperking %d', $index + 1),
56 6
            Direction::from($object->properties->direction),
57 6
            !empty($object->properties->transportMode) ? TransportMode::from($object->properties->transportMode) : null,
58 6
            RestrictionType::from($object->properties->restrictionType),
59 6
            array_map([VehicleType::class, 'from'], $object->properties->vehicleTypes ?? $object->properties->vehicles ?? []),
60 3
            $emergencyServices,
61 3
            $publicTransport,
62 6
            $object->properties->maximumSpeed ?? null,
63 3
            $laneInformation,
64 3
            $vehicleInformation,
65 6
            property_exists($object->properties, 'roadManagementTypes')
66 6
                ? array_map([RoadManagementType::class, 'from'], $object->properties->roadManagementTypes)
67 6
                : null,
68 6
            ($object->properties->roadManagementType ?? null) ? RoadManagementType::from($object->properties->roadManagementType) : null
69 3
        );
70
    }
71
72 6
    protected function parseExtraInfo(\stdClass $object): RestrictionExtraInfo
73
    {
74 6
        $passageAllowed = $object->passageAllowed ?? null;
75
76 6
        if ($passageAllowed === 'true' || $passageAllowed === 'YES') {
77 6
            $passageAllowed = true;
78 6
        } elseif ($passageAllowed === 'false' || $passageAllowed === 'NO') {
79 6
            $passageAllowed = false;
80
        } else {
81 6
            $passageAllowed = null;
82
        }
83
84 6
        return new RestrictionExtraInfo(
85 6
            $passageAllowed,
86 6
            $object->description ?? ''
87 3
        );
88
    }
89
}
90