Completed
Pull Request — master (#472)
by
unknown
02:37
created

EventType   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 39
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A deserialize() 0 4 1
A fromJSONLDEvent() 0 10 3
A fromUdb3ModelCategory() 0 13 2
1
<?php
2
3
namespace CultuurNet\UDB3\Event;
4
5
use CultuurNet\UDB3\Category;
6
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Category\Category as Udb3ModelCategory;
7
use InvalidArgumentException;
8
9
final class EventType extends Category
10
{
11
    public const DOMAIN = 'eventtype';
12
13
    public function __construct(string $id, string $label)
14
    {
15
        parent::__construct($id, $label, self::DOMAIN);
16
    }
17
18
    public static function deserialize(array $data): EventType
19
    {
20
        return new self($data['id'], $data['label']);
21
    }
22
23
    public static function fromJSONLDEvent(string $eventString): ?EventType
24
    {
25
        $event = json_decode($eventString, false);
26
        foreach ($event->terms as $term) {
27
            if ($term->domain === self::DOMAIN) {
28
                return new self($term->id, $term->label);
29
            }
30
        }
31
        return null;
32
    }
33
34
    public static function fromUdb3ModelCategory(Udb3ModelCategory $category): EventType
35
    {
36
        $label = $category->getLabel();
37
38
        if (is_null($label)) {
39
            throw new InvalidArgumentException('Category label is required.');
40
        }
41
42
        return new self(
43
            $category->getId()->toString(),
44
            $label->toString()
45
        );
46
    }
47
}
48