DoNotDelete   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setDeletedAt() 0 12 6
A canNotDelete() 0 4 1
A getDeletedAt() 0 13 6
1
<?php
2
3
namespace ETNA\Doctrine\Extensions;
4
5
use Doctrine\ORM\Mapping\Column;
6
use Doctrine\ORM\Mapping\PreRemove;
7
8
trait DoNotDelete
9
{
10
    /**
11
     * @Column(type="datetime", name="deleted_at", nullable=true)
12
     */
13
    private $deleted_at;
14
15
    /**
16
     * Getter
17
     */
18
    public function getDeletedAt($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...
19
    {
20
        switch (true) {
21
            case is_string($this->deleted_at):
22
            case is_object($this->deleted_at) && get_class($this->deleted_at) !== 'DateTime':
23
                throw new \Exception("deleted_at is not a datetime", 400);
24
            case $this->deleted_at === null:
25
            case $format === null:
26
                return $this->deleted_at;
27
            default:
28
                return $this->deleted_at->format($format);
29
        }
30
    }
31
32
    /**
33
     * Setter
34
     */
35
    public function setDeletedAt($datetime)
36
    {
37
        switch (true) {
38
            case is_object($datetime) && get_class($datetime) !== 'DateTime':
39
            case is_string($datetime) && !preg_match("#^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$#", trim($datetime)):
40
                throw new \Exception("bad deleted_at provided", 400);
41
            default:
42
                $this->deleted_at = (is_object($datetime)) ? $datetime : new \DateTime($datetime);
43
        }
44
45
        return $this;
46
    }
47
48
    /**
49
     * @PreRemove
50
     */
51
    public function canNotDelete()
52
    {
53
        throw new \Exception("Cannot delete " . get_class($this));
54
    }
55
}
56