Completed
Push — master ( 58620c...f23d9f )
by Vitaliy
02:20
created

Expiration   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 88
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A storeTo() 0 8 1
A notStore() 0 4 1
A fromNull() 0 7 1
A isNull() 0 4 1
A getValue() 0 12 3
A __construct() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the AppleApnPush package
5
 *
6
 * (c) Vitaliy Zhuk <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code
10
 */
11
12
namespace Apple\ApnPush\Model;
13
14
/**
15
 * Expiration of message
16
 */
17
class Expiration
18
{
19
    /**
20
     * @var \DateTime
21
     */
22
    private $storeTo;
23
24
    /**
25
     * @var bool
26
     */
27
    private $null;
28
29
    /**
30
     * Create new expiration with store message to time
31
     *
32
     * @param \DateTime $availableTo
33
     *
34
     * @return Expiration
35
     */
36
    public static function storeTo(\DateTime $availableTo) : Expiration
37
    {
38
        $expiration = new self();
39
        $expiration->storeTo = clone $availableTo;
40
        $expiration->storeTo->setTimezone(new \DateTimeZone('UTC'));
41
42
        return $expiration;
43
    }
44
45
    /**
46
     * Create new expiration without store message
47
     *
48
     * @return Expiration
49
     */
50
    public static function notStore() : Expiration
51
    {
52
        return new self();
53
    }
54
55
    /**
56
     * Create expiration from null
57
     *
58
     * @return Expiration
59
     */
60
    public static function fromNull() : Expiration
61
    {
62
        $expiration = new self();
63
        $expiration->null = true;
64
65
        return $expiration;
66
    }
67
68
    /**
69
     * Is null object?
70
     *
71
     * @return bool
72
     */
73
    public function isNull()
74
    {
75
        return (bool) $this->null;
76
    }
77
78
    /**
79
     * Get value
80
     *
81
     * @return int
82
     *
83
     * @throws \LogicException
84
     */
85
    public function getValue() : int
86
    {
87
        if ($this->isNull()) {
88
            throw new \LogicException('Can not get value of expiration from null value.');
89
        }
90
91
        if (!$this->storeTo) {
92
            return 0;
93
        }
94
95
        return $this->storeTo->format('U');
96
    }
97
98
    /**
99
     * Constructor.
100
     */
101
    private function __construct()
102
    {
103
    }
104
}
105