TimestampTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 42
rs 10
c 2
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateTimestamps() 0 5 2
A getLastModified() 0 3 1
A getCreationDate() 0 3 1
1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Entity;
20
21
use DateTime;
22
use Doctrine\ORM\Mapping as ORM;
23
24
/**
25
 * @ORM\HasLifecycleCallbacks()
26
 */
27
trait TimestampTrait
28
{
29
    /**
30
     * @var DateTime the date when this element was modified the last time
31
     * @ORM\Column(type="datetime", options={"default"="CURRENT_TIMESTAMP"})
32
     */
33
    protected $last_modified;
34
35
    /**
36
     * @var DateTime the date when this element was created
37
     * @ORM\Column(type="datetime", options={"default"="CURRENT_TIMESTAMP"})
38
     */
39
    protected $creation_date;
40
41
    /**
42
     * Helper for updating the timestamp. It is automatically called by doctrine before persisting.
43
     *
44
     * @ORM\PrePersist
45
     * @ORM\PreUpdate
46
     */
47
    public function updateTimestamps(): void
48
    {
49
        $this->last_modified = new DateTime('now');
50
        if (null === $this->creation_date) {
51
            $this->creation_date = new DateTime('now');
52
        }
53
    }
54
55
    /**
56
     * Returns the datetime this element was created. Returns null, if it was not persisted yet.
57
     */
58
    public function getCreationDate(): ?DateTime
59
    {
60
        return $this->creation_date;
61
    }
62
63
    /**
64
     * Returns the datetime this element was last time modified. Returns null, if it was not persisted yet.
65
     */
66
    public function getLastModified(): ?DateTime
67
    {
68
        return $this->last_modified;
69
    }
70
}
71