DateCreatedAttribute   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
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 55
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

3 Methods

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