Completed
Push — master ( 533677...033377 )
by Jean
12s
created

UpdatedAt::setUpdatedAtBeforeUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ETNA\Doctrine\Extensions;
4
5
use Doctrine\ORM\Mapping\Column;
6
use Doctrine\ORM\Mapping\PrePersist;
7
use Doctrine\ORM\Mapping\PreUpdate;
8
9 View Code Duplication
trait UpdatedAt
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
    /**
12
     * @Column(type="datetime", name="updated_at")
13
     */
14
    private $updated_at;
15
16
    /**
17
     * Getter
18
     */
19
    public function getUpdatedAt($format = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
20
    {
21
        switch (true) {
22
            case is_string($this->updated_at):
23
            case is_object($this->updated_at) && get_class($this->updated_at) !== 'DateTime':
24
                throw new \Exception("updated_at is not a datetime", 400);
25
            case $this->updated_at === null:
26
            case $format === null:
27
                return $this->updated_at;
28
            default:
29
                return $this->updated_at->format($format);
30
        }
31
    }
32
33
    /**
34
     * @PreUpdate
35
     * @PrePersist
36
     */
37
    public function setUpdatedAt()
38
    {
39
        $this->updated_at = new \DateTime(date('Y-m-d H:i:s'));
40
    }
41
}
42