DateTimeDeletedTrait::setDeletedAt()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RedRat\Entity;
6
7
use DateTimeInterface;
8
use Exception;
9
10
/**
11
 * DateTimeDeleted Trait
12
 *
13
 * @package RedRat\Entity
14
 */
15
trait DateTimeDeletedTrait
16
{
17
    /**
18
     * @var DateTimeInterface
19
     */
20
    private $deletedAt;
21
22
    /**
23
     * @var string
24
     */
25
    protected static $deletedAtStringFormat = 'Y-m-d H:i:s';
26
27
    /**
28
     * @return DateTimeInterface|null
29
     */
30 8
    public function getDeletedAt(): ?DateTimeInterface
31
    {
32 8
        return $this->deletedAt;
33
    }
34
35
    /**
36
     * @param string|null $format
37
     * @return string|null
38
     */
39 3
    public function getDeletedAtString(?string $format = null): ?string
40
    {
41 3
        if (!$this->hasDeletedAt()) {
42 1
            return null;
43
        }
44
45
        return $this
46 2
            ->getDeletedAt()
47 2
            ->format($format ?? self::$deletedAtStringFormat)
48
        ;
49
    }
50
51
    /**
52
     * @return bool
53
     */
54 5
    public function hasDeletedAt(): bool
55
    {
56 5
        return $this->getDeletedAt() instanceof DateTimeInterface;
57
    }
58
59
    /**
60
     * @param DateTimeInterface $deletedAt
61
     * @return void
62
     */
63 5
    public function setDeletedAt(DateTimeInterface $deletedAt): void
64
    {
65 5
        $this->deletedAt = $deletedAt;
66 5
    }
67
68
    /**
69
     * @return void
70
     * @throws Exception
71
     */
72 1
    public function forgeDeletedAt(): void
73
    {
74 1
        $this->setDeletedAt(
75 1
            new \DateTime('now')
76
        );
77 1
    }
78
}
79