Completed
Push — master ( 7ff0dd...2ef7db )
by Jonas
31s queued 11s
created

Timestamp   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A getStartDate() 0 4 1
A getEndDate() 0 4 1
A getStatus() 0 4 1
A deserialize() 0 13 2
A serialize() 0 8 1
A toJsonLd() 0 7 1
A fromUdb3ModelSubEvent() 0 8 1
1
<?php
2
3
namespace CultuurNet\UDB3;
4
5
use Broadway\Serializer\SerializableInterface;
6
use CultuurNet\UDB3\Event\ValueObjects\Status;
7
use CultuurNet\UDB3\Event\ValueObjects\StatusType;
8
use CultuurNet\UDB3\Model\ValueObject\Calendar\SubEvent;
9
use DateTime;
10
use DateTimeInterface;
11
use InvalidArgumentException;
12
13
final class Timestamp implements SerializableInterface
14
{
15
    /**
16
     * @var DateTimeInterface
17
     */
18
    private $startDate;
19
20
    /**
21
     * @var DateTimeInterface
22
     */
23
    private $endDate;
24
25
    /**
26
     * @var Status
27
     */
28
    private $status;
29
30
    final public function __construct(
31
        DateTimeInterface $startDate,
32
        DateTimeInterface $endDate,
33
        Status $status = null
34
    ) {
35
        if ($endDate < $startDate) {
36
            throw new InvalidArgumentException('End date can not be earlier than start date.');
37
        }
38
39
        $this->startDate = $startDate;
40
        $this->endDate = $endDate;
41
        $this->status = $status ?? new Status(StatusType::available(), []);
42
    }
43
44
    public function getStartDate(): DateTimeInterface
45
    {
46
        return $this->startDate;
47
    }
48
49
    public function getEndDate(): DateTimeInterface
50
    {
51
        return $this->endDate;
52
    }
53
54
    public function getStatus(): Status
55
    {
56
        return $this->status;
57
    }
58
59
    public static function deserialize(array $data): Timestamp
60
    {
61
        $status = null;
62
        if (isset($data['status'])) {
63
            $status = Status::deserialize($data['status']);
64
        }
65
66
        return new static(
67
            DateTime::createFromFormat(DateTime::ATOM, $data['startDate']),
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...OM, $data['startDate']) targeting DateTime::createFromFormat() can also be of type false; however, CultuurNet\UDB3\Timestamp::__construct() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?
Loading history...
68
            DateTime::createFromFormat(DateTime::ATOM, $data['endDate']),
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...ATOM, $data['endDate']) targeting DateTime::createFromFormat() can also be of type false; however, CultuurNet\UDB3\Timestamp::__construct() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?
Loading history...
69
            $status
70
        );
71
    }
72
73
    public function serialize(): array
74
    {
75
        return [
76
            'startDate' => $this->startDate->format(DateTime::ATOM),
77
            'endDate' => $this->endDate->format(DateTime::ATOM),
78
            'status' => $this->status->serialize(),
79
        ];
80
    }
81
82
    public function toJsonLd(): array
83
    {
84
        $jsonLd = $this->serialize();
85
        $jsonLd['@type'] = 'Event';
86
87
        return $jsonLd;
88
    }
89
90
    public static function fromUdb3ModelSubEvent(SubEvent $subEvent): Timestamp
91
    {
92
        return new Timestamp(
93
            $subEvent->getDateRange()->getFrom(),
94
            $subEvent->getDateRange()->getTo(),
95
            Status::fromUdb3ModelStatus($subEvent->getStatus())
96
        );
97
    }
98
}
99