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

EventStatus::toJsonLd()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
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