Completed
Push — develop ( 11971b...45f15f )
by Nate
06:59
created

DateUpdatedMutatorTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 50
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setDateUpdated() 0 10 3
A getDateUpdated() 0 10 2
A getDateUpdatedIso8601() 0 15 3
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\objects;
10
11
use craft\helpers\DateTimeHelper;
12
use DateTime;
13
14
/**
15
 * @property DateTime|null $dateUpdated
16
 *
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 2.0.0
19
 */
20
trait DateUpdatedMutatorTrait
21
{
22
    /**
23
     * @param $value
24
     * @return $this
25
     */
26
    public function setDateUpdated($value)
27
    {
28
        if ($value) {
29
            $value = DateTimeHelper::toDateTime($value);
30
        }
31
32
        $this->dateUpdated = $value ?: null;
33
34
        return $this;
35
    }
36
37
    /**
38
     * @return DateTime
39
     */
40
    public function getDateUpdated()
41
    {
42
        if (empty($this->dateUpdated)) {
43
            return DateTimeHelper::toDateTime(
44
                new DateTime('now')
45
            );
46
        }
47
48
        return $this->dateUpdated;
49
    }
50
51
    /**
52
     * @return string|null
53
     */
54
    public function getDateUpdatedIso8601()
55
    {
56
57
        // Get the datetime
58
        if (!$dateCreated = $this->getDateUpdated()) {
59
            return null;
60
        }
61
62
        // Convert it to iso
63
        if (!$iso = DateTimeHelper::toIso8601($dateCreated)) {
64
            return null;
65
        }
66
67
        return $iso;
68
    }
69
}
70