TimeLimitManager::setBufferInSeconds()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2014-07-23
5
 */
6
7
namespace Net\Bazzline\Component\TimeLimitManager;
8
9
/**
10
 * Class TimeLimitManager
11
 * @package Net\Bazzline\Component\TimeLimitManager
12
 */
13
class TimeLimitManager
14
{
15
    /**
16
     * @var int
17
     */
18
    private $bufferInSeconds;
19
20
    /**
21
     * @var int
22
     */
23
    private $limitFromIniInSeconds;
24
25
    /**
26
     * @var int
27
     */
28
    private $limitInSeconds;
29
30
    /**
31
     * @var int
32
     */
33
    private $startTime;
34
35
    public function __construct()
36
    {
37
        $this->limitFromIniInSeconds = (int) ini_get('max_execution_time');
38
        $this->setLimitInSeconds($this->limitFromIniInSeconds);
39
        $this->startTime = time();
40
    }
41
42
    /**
43
     * @param int $seconds
44
     */
45
    public function setBufferInSeconds($seconds)
46
    {
47
        $this->bufferInSeconds = (int) $seconds;
48
    }
49
50
    /**
51
     * @param int $minutes
52
     */
53
    public function setBufferInMinutes($minutes)
54
    {
55
        $this->setBufferInSeconds((60 * $minutes));
56
    }
57
58
    /**
59
     * @param int $hours
60
     */
61
    public function setBufferInHours($hours)
62
    {
63
        $this->setBufferInMinutes((60 * $hours));
64
    }
65
66
    /**
67
     * @return int
68
     */
69
    public function getBufferInSeconds()
70
    {
71
        return $this->bufferInSeconds;
72
    }
73
74
    /**
75
     * @param int $seconds
76
     * @throws InvalidArgumentException
77
     */
78
    public function setLimitInSeconds($seconds)
79
    {
80
        if ($this->limitFromIniInSeconds > 0) {
81
            if ($seconds > $this->limitFromIniInSeconds) {
82
                throw new InvalidArgumentException(
83
                    'provided limit (' . $seconds .
84
                    ') is above ini limit (' .
85
                    $this->limitFromIniInSeconds . ')',
86
                    1
87
                );
88
            }
89
        }
90
91
        $this->limitInSeconds = time() + (int) $seconds;
92
    }
93
94
    /**
95
     * @param int $minutes
96
     * @throws InvalidArgumentException
97
     */
98
    public function setLimitInMinutes($minutes)
99
    {
100
        $this->setLimitInSeconds((60 * $minutes));
101
    }
102
103
    /**
104
     * @param int $hours
105
     * @throws InvalidArgumentException
106
     */
107
    public function setLimitInHours($hours)
108
    {
109
        $this->setLimitInMinutes((60 * $hours));
110
    }
111
112
    /**
113
     * @return int
114
     */
115
    public function getLimitInSeconds()
116
    {
117
        return $this->limitInSeconds;
118
    }
119
120
121
    /**
122
     * @return bool
123
     */
124
    public function isLimitReached()
125
    {
126
        $currentTimeWithBuffer = time() + $this->bufferInSeconds;
127
128
        $isReached = ($currentTimeWithBuffer >= $this->limitInSeconds);
129
130
        return $isReached;
131
    }
132
} 
133