Completed
Pull Request — master (#482)
by
unknown
02:01
created

EventStatus   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 28.24 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 24
loc 85
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getEventStatusType() 0 4 1
A getEventStatusReasons() 0 4 1
A deserialize() 0 15 2
A serialize() 12 12 2
A toJsonLd() 12 12 2
A ensureTranslationsAreUnique() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
3
namespace CultuurNet\UDB3\Event\ValueObjects;
4
5
use Broadway\Serializer\SerializableInterface;
6
use CultuurNet\UDB3\Model\ValueObject\Translation\Language;
7
use InvalidArgumentException;
8
9
final class EventStatus implements SerializableInterface
10
{
11
    /**
12
     * @var EventStatusType
13
     */
14
    private $eventStatusType;
15
16
    /**
17
     * @var EventStatusReason[]
18
     */
19
    private $eventStatusReasons;
20
21
    public function __construct(EventStatusType $eventStatusType, array $eventStatusReasons)
22
    {
23
        $this->ensureTranslationsAreUnique($eventStatusReasons);
24
        $this->eventStatusType = $eventStatusType;
25
        $this->eventStatusReasons = $eventStatusReasons;
26
    }
27
28
    public function getEventStatusType(): EventStatusType
29
    {
30
        return $this->eventStatusType;
31
    }
32
33
    public function getEventStatusReasons(): array
34
    {
35
        return $this->eventStatusReasons;
36
    }
37
38
    public static function deserialize(array $data): EventStatus
39
    {
40
        $eventStatusReasons = [];
41
        foreach ($data['eventStatusReason'] as $language => $eventStatusReason) {
42
            $eventStatusReasons[] = new EventStatusReason(
43
                new Language($language),
44
                $eventStatusReason
45
            );
46
        }
47
48
        return new EventStatus(
49
            EventStatusType::fromNative($data['eventStatus']),
50
            $eventStatusReasons
51
        );
52
    }
53
54 View Code Duplication
    public function serialize(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $eventStatusReasons = [];
57
        foreach ($this->eventStatusReasons as $statusReason) {
58
            $eventStatusReasons[$statusReason->getLanguage()->getCode()] = $statusReason->getReason();
59
        }
60
61
        return [
62
            'eventStatus' => $this->eventStatusType->toNative(),
63
            'eventStatusReason' => $eventStatusReasons,
64
        ];
65
    }
66
67 View Code Duplication
    public function toJsonLd(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        $eventStatusReasons = [];
70
        foreach ($this->eventStatusReasons as $statusReason) {
71
            $eventStatusReasons[$statusReason->getLanguage()->getCode()] = $statusReason->getReason();
72
        }
73
74
        return array_filter([
75
            'eventStatus' => 'https://schema.org/' . $this->eventStatusType->toNative(),
76
            'eventStatusReason' => $eventStatusReasons ?? null,
77
        ]);
78
    }
79
80
    /**
81
     * @param EventStatusReason[] $eventStatusReasons
82
     */
83
    private function ensureTranslationsAreUnique(array $eventStatusReasons): void
84
    {
85
        $languageCodes = \array_map(static function (EventStatusReason $reason) {
86
            return $reason->getLanguage()->getCode();
87
        }, $eventStatusReasons);
88
89
        if (count($languageCodes) !== count(array_unique($languageCodes))) {
90
            throw new InvalidArgumentException('Duplicate translations are not allowed for EventStatusReason');
91
        }
92
    }
93
}
94