Completed
Push — master ( ff11b3...fd6c95 )
by Damien
10:30
created

MetadataOptions   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setExpiryInterval() 0 17 3
A getExpiryInterval() 0 4 1
A getExpiryDate() 0 4 1
A setExpiryDate() 0 10 2
A jsonSerialize() 0 12 2
1
<?php
2
3
4
namespace flipbox\saml\core\models;
5
6
use yii\base\Model;
7
8
class MetadataOptions extends Model implements \JsonSerializable
9
{
10
    use JsonModel;
11
    /**
12
     * @var string
13
     */
14
    public $url;
15
16
    /**
17
     * @var string
18
     */
19
    protected $expiryInterval;
20
21
    /**
22
     * @var \DateTime
23
     */
24
    protected $expiryDate;
25
26
    /**
27
     * @param string|null $expiry
28
     * @return $this
29
     */
30
    public function setExpiryInterval($expiry)
31
    {
32
        if (! $expiry) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $expiry of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
33
            $this->expiryDate = null;
34
            return $this;
35
        }
36
37
        $this->expiryInterval = $expiry;
38
39
        $now = new \DateTime();
40
41
        if (is_null($this->expiryDate)) {
42
            $this->expiryDate = $now->add(new \DateInterval($this->expiryInterval));
43
        }
44
45
        return $this;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getExpiryInterval()
52
    {
53
        return $this->expiryInterval;
54
    }
55
56
    /**
57
     * @return \DateTime
58
     */
59
    public function getExpiryDate()
60
    {
61
        return $this->expiryDate;
62
    }
63
64
    /**
65
     * @param string|null $expiry
66
     * @return \DateTime
67
     */
68
    public function setExpiryDate($expiry)
69
    {
70
        if (! $expiry) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $expiry of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
71
            return $this;
72
        }
73
74
        $this->expiryDate = new \DateTime($expiry);
75
76
        return $this;
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function jsonSerialize()
83
    {
84
        $expiryDate = null;
85
        if ($this->expiryDate instanceof \DateTime) {
86
            $expiryDate = $this->expiryDate->format(\DateTime::ISO8601);
87
        }
88
        return [
89
            'expiryDate' => $expiryDate,
90
            'expiryInterval' => $this->getExpiryInterval(),
91
            'url' => $this->url,
92
        ];
93
    }
94
}
95