Completed
Pull Request — master (#345)
by
unknown
05:39
created

Timestamp::getStartDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace CultuurNet\UDB3;
4
5
use Broadway\Serializer\SerializableInterface;
6
use CultuurNet\UDB3\Model\ValueObject\Calendar\DateRange;
7
use DateTime;
8
use DateTimeInterface;
9
use InvalidArgumentException;
10
11
/**
12
 * Provices a class for a timestamp.
13
 * @todo Replace by CultuurNet\UDB3\Model\ValueObject\Calendar\DateRange.
14
 */
15
class Timestamp implements SerializableInterface
16
{
17
18
    /**
19
     * @var DateTimeInterface
20
     */
21
    protected $startDate;
22
23
    /**
24
     * @var DateTimeInterface
25
     */
26
    protected $endDate;
27
28
    /**
29
     * Constructor
30
     *
31
     * @param DateTimeInterface $startDate
32
     * @param DateTimeInterface $endDate
33
     *
34
     * @throws InvalidArgumentException
35
     */
36
    public function __construct(
37
        DateTimeInterface $startDate,
38
        DateTimeInterface $endDate
39
    ) {
40
        if ($endDate < $startDate) {
41
            throw new InvalidArgumentException('End date can not be earlier than start date.');
42
        }
43
44
        $this->startDate = $startDate;
45
        $this->endDate = $endDate;
46
    }
47
48
    /**
49
     * @return DateTimeInterface
50
     */
51
    public function getStartDate()
52
    {
53
        return $this->startDate;
54
    }
55
56
    /**
57
     * @return DateTimeInterface
58
     */
59
    public function getEndDate()
60
    {
61
        return $this->endDate;
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public static function deserialize(array $data)
68
    {
69
        return new static(
70
            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...
71
            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...
72
        );
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function serialize()
79
    {
80
        return [
81
            'startDate' => $this->startDate->format(DateTime::ATOM),
82
            'endDate' => $this->endDate->format(DateTime::ATOM),
83
        ];
84
    }
85
86
    /**
87
     * @param DateRange $dateRange
88
     * @return self
89
     */
90
    public static function fromUdb3ModelDateRange(DateRange $dateRange)
91
    {
92
        return new Timestamp(
93
            $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...
94
            $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...
95
        );
96
    }
97
}
98