Completed
Push — master ( bcdba8...408d98 )
by Jan
03:54
created

TimestampTrait::getLastModified()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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