Passed
Push — master ( 08267b...db956f )
by Jan
04:49
created

TimestampTrait::updateTimestamps()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\Entity\Base;
44
45
use DateTime;
46
use Symfony\Component\Serializer\Annotation\Groups;
47
48
/**
49
 * A entity with these trait contains informations about, when it was created and edited last time.
50
 */
51
trait TimestampTrait
52
{
53
    /**
54
     * @var DateTime the date when this element was modified the last time
55
     * @ORM\Column(type="datetime", name="last_modified", options={"default"="CURRENT_TIMESTAMP"})
56
     * @Groups({"extended", "full"})
57
     */
58
    protected $lastModified;
59
60
    /**
61
     * @var DateTime the date when this element was created
62
     * @ORM\Column(type="datetime", name="datetime_added", options={"default"="CURRENT_TIMESTAMP"})
63
     * @Groups({"extended", "full"})
64
     */
65
    protected $addedDate;
66
67
    /**
68
     * Returns the last time when the element was modified.
69
     * Returns null if the element was not yet saved to DB yet.
70
     *
71
     * @return DateTime|null the time of the last edit
72
     */
73
    public function getLastModified(): ?DateTime
74
    {
75
        return $this->lastModified;
76
    }
77
78
    /**
79
     * Returns the date/time when the element was created.
80
     * Returns null if the element was not yet saved to DB yet.
81
     *
82
     * @return DateTime|null the creation time of the part
83
     */
84
    public function getAddedDate(): ?DateTime
85
    {
86
        return $this->addedDate;
87
    }
88
89
    /**
90
     * Helper for updating the timestamp. It is automatically called by doctrine before persisting.
91
     *
92
     * @ORM\PrePersist
93
     * @ORM\PreUpdate
94
     */
95
    public function updateTimestamps(): void
96
    {
97
        $this->lastModified = new DateTime('now');
98
        if (null === $this->addedDate) {
99
            $this->addedDate = new DateTime('now');
100
        }
101
    }
102
}
103