Completed
Push — develop ( 15ff77...16a829 )
by
unknown
12s
created

setDateModified()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 8.8571
cc 6
eloc 12
nc 10
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** */
11
namespace Core\Entity;
12
13
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
14
use DateTime;
15
use InvalidArgumentException;
16
use Exception;
17
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
18
19
/**
20
 * Implementation stub for DateAware entities.
21
 *
22
 * Since this class uses Doctrines' PrePersist and PreUpdate annotations,
23
 * you must annotate any class using this trait with the __@HasLifecycleCallbacks__
24
 *
25
 * @author Mathias Gelhausen <[email protected]>
26
 * @author Miroslav Fedeleš <[email protected]>
27
 * @see \Core\Entity\ModificationDateAwareEntityInterface
28
 */
29
trait ModificationDateAwareEntityTrait
30
{
31
    /**
32
     * Creation date.
33
     *
34
     * @var DateTime
35
     * @ODM\Field(type="tz_date")
36
     */
37
    protected $dateCreated;
38
    
39
    /**
40
     * Modification date.
41
     *
42
     * @var DateTime
43
     * @ODM\Field(type="tz_date")
44
     */
45
    protected $dateModified;
46
    
47
    public function getDateCreated()
48
    {
49
        return $this->dateCreated;
50
    }
51
52
    /**
53
     * @see \Core\Entity\ModificationDateAwareEntityInterface::setDateCreated()
54
     * @ODM\PrePersist
55
     */
56
    public function setDateCreated($dateCreated = null)
57
    {
58
        if (!isset($dateCreated) || $dateCreated instanceof LifecycleEventArgs) {
59
            $dateCreated = new DateTime();
60
        }
61
        if (!$dateCreated instanceof DateTime) {
62
            throw new InvalidArgumentException(sprintf('$dateCreated has to be null, %s or %s', DateTime::class, LifecycleEventArgs::class));
63
        }
64
        $this->dateCreated = $dateCreated;
65
        return $this;
66
    }
67
68
    /**
69
     * @see \Core\Entity\ModificationDateAwareEntityInterface::getDateModified()
70
     * @ODM\PrePersist
71
     */
72
    public function getDateModified()
73
    {
74
        return $this->dateModified;
75
    }
76
77
    /**
78
     * @see \Core\Entity\ModificationDateAwareEntityInterface::getDateModified()
79
     * @ODM\PrePersist
80
     * @ODM\PreUpdate
81
     */
82
    public function setDateModified($dateModified = null)
83
    {
84
        if (!isset($dateModified) || $dateModified instanceof LifecycleEventArgs) {
85
            $dateModified = new DateTime();
86
        }
87
        if (is_string($dateModified)) {
88
            try {
89
                $dateModified = new DateTime($dateModified);
90
            } catch (Exception $e) {
91
                throw new InvalidArgumentException('Invalid date string', 0, $e);
92
            }
93
        }
94
        if (!$dateModified instanceof DateTime) {
95
            throw new InvalidArgumentException(sprintf('$dateModified has to be null, string, %s or %s', DateTime::class, LifecycleEventArgs::class));
96
        }
97
        $this->dateModified = $dateModified;
98
        return $this;
99
    }
100
}
101