Passed
Push — master ( 67ee52...70965b )
by Mathieu
02:28
created

TimestampableTrait::lastModified()   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
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Charcoal\Object;
4
5
use InvalidArgumentException;
6
7
use DateTime;
8
use DateTimeInterface;
9
10
trait TimestampableTrait
11
{
12
    /**
13
     * Object creation date (set automatically on save)
14
     * @var DateTimeInterface
15
     */
16
    private $created;
17
18
    /**
19
     * Object last modified date (set automatically on save and update)
20
     * @var DateTimeInterface
21
     */
22
    private $lastModified;
23
24
    /**
25
     * @param \DateTimeInterface|string|null $created The object's creation timestamp.
26
     * @throws InvalidArgumentException If the provided date/time is invalid.
27
     * @return self
28
     */
29 View Code Duplication
    public function setCreated($created)
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...
30
    {
31
        if ($created === null) {
32
            $this->created = null;
33
            return $this;
34
        }
35
        if (is_string($created)) {
36
            $created = new DateTime($created);
37
        }
38
        if (!($created instanceof DateTimeInterface)) {
39
            throw new InvalidArgumentException(
40
                'Invalid "Created" value. Must be a date/time string or a DateTime object.'
41
            );
42
        }
43
        $this->created = $created;
44
        return $this;
45
    }
46
47
    /**
48
     * @return DateTimeInterface|null
49
     */
50
    public function created()
51
    {
52
        return $this->created;
53
    }
54
55
    /**
56
     * @param \DateTimeInterface|string|null $lastModified The object's last modified timestamp.
57
     * @throws InvalidArgumentException If the provided date/time is invalid.
58
     * @return self
59
     */
60 View Code Duplication
    public function setLastModified($lastModified)
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...
61
    {
62
        if ($lastModified === null) {
63
            $this->lastModified = null;
64
            return $this;
65
        }
66
        if (is_string($lastModified)) {
67
            $lastModified = new DateTime($lastModified);
68
        }
69
        if (!($lastModified instanceof DateTimeInterface)) {
70
            throw new InvalidArgumentException(
71
                'Invalid "Last Modified" value. Must be a date/time string or a DateTime object.'
72
            );
73
        }
74
        $this->lastModified = $lastModified;
75
        return $this;
76
    }
77
78
    /**
79
     * @return DateTimeInterface|null
80
     */
81
    public function lastModified()
82
    {
83
        return $this->lastModified;
84
    }
85
}
86