Completed
Pull Request — master (#480)
by Jonas
02:19
created

Timestamp::getEventStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace CultuurNet\UDB3;
4
5
use Broadway\Serializer\SerializableInterface;
6
use CultuurNet\UDB3\Event\ValueObjects\EventStatus;
7
use CultuurNet\UDB3\Event\ValueObjects\EventStatusType;
8
use CultuurNet\UDB3\Model\ValueObject\Calendar\DateRange;
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 EventStatus|null
27
     */
28
    private $eventStatus;
29
30
    final public function __construct(
31
        DateTimeInterface $startDate,
32
        DateTimeInterface $endDate,
33
        EventStatus $eventStatus = null
0 ignored issues
show
Unused Code introduced by
The parameter $eventStatus is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
    }
42
43
    public function getStartDate(): DateTimeInterface
44
    {
45
        return $this->startDate;
46
    }
47
48
    public function getEndDate(): DateTimeInterface
49
    {
50
        return $this->endDate;
51
    }
52
53
    public function getEventStatus(): ?EventStatus
54
    {
55
        return $this->eventStatus;
56
    }
57
58
    public static function deserialize(array $data): Timestamp
59
    {
60
        return new static(
61
            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...
62
            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...
63
        );
64
    }
65
66
    public function serialize(): array
67
    {
68
        $serialized = [
69
            'startDate' => $this->startDate->format(DateTime::ATOM),
70
            'endDate' => $this->endDate->format(DateTime::ATOM),
71
            'eventStatus' => EventStatusType::scheduled()->toNative()
72
        ];
73
74
        if ($this->eventStatus === null) {
75
            return $serialized;
76
        }
77
78
        return \array_merge(
79
            $serialized,
80
            $this->eventStatus->serialize()
81
        );
82
    }
83
84
    public static function fromUdb3ModelDateRange(DateRange $dateRange): Timestamp
85
    {
86
        return new Timestamp(
87
            $dateRange->getFrom(),
0 ignored issues
show
Bug introduced by
It seems like $dateRange->getFrom() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
88
            $dateRange->getTo()
0 ignored issues
show
Bug introduced by
It seems like $dateRange->getTo() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
89
        );
90
    }
91
}
92