Passed
Push — master ( 6a730c...54bd4a )
by Vince
01:27
created

limiterOptions::isUnlimited()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * ==================================
4
 * Responsible PHP API
5
 * ==================================
6
 *
7
 * @link Git https://github.com/vince-scarpa/responsibleAPI.git
8
 *
9
 * @api Responible API
10
 * @package responsible\core\throttle
11
 *
12
 * @author Vince scarpa <[email protected]>
13
 *
14
 */
15
namespace responsible\core\throttle;
16
17
use responsible\core\server;
18
use responsible\core\exception;
19
20
class limiterOptions
21
{
22
    use \responsible\core\traits\optionsTrait;
23
24
    /**
25
     * [$capacity Bucket volume]
26
     * @var integer
27
     */
28
    protected $capacity = 100;
29
30
    /**
31
     * [$leakRate Constant rate at which the bucket will leak]
32
     * @var string|integer
33
     */
34
    protected $leakRate = 1;
35
36
    /**
37
     * [$timeframe Durations are in seconds]
38
     * @var array
39
     */
40
    protected static $timeframe = [
41
        'SECOND' => 1,
42
        'MINUTE' => 60,
43
        'HOUR' => 3600,
44
        'DAY' => 86400,
45
        'CUSTOM' => 0,
46
    ];
47
48
    /**
49
     * [$window Timeframe window]
50
     * @var integer|string
51
     */
52
    protected $window = 'MINUTE';
53
54
    /**
55
     * [$unlimited Rate limiter bypass if true]
56
     * @var boolean
57
     */
58
    protected $unlimited = false;
59
60
    /**
61
     * [$account User account object]
62
     */
63
    protected $account;
64
65
    /**
66
     * [$isMockTest Set the mock test]
67
     * @var boolean
68
     */
69
    protected $isMockTest = false;
70
71
    /**
72
     * [$isMockTest Set the mock account]
73
     * @var array
74
     */
75
    protected $mockAccount = [];
76
77
    /**
78
     * [$scope Set the default scope]
79
     * @var string
80
     */
81
    protected $scope = 'private';
82
83
    /**
84
     * [hasOptionProperty Check if an option property is set]
85
     * @param  array  $options
86
     * @param  string  $property
87
     * @return string|integer|boolean
88
     */
89
    protected function hasOptionProperty(array $options, $property, $default = false)
90
    {
91
        $val = isset($options[$property]) ? $options[$property] : $default;
92
93
        if ($val && empty($options[$property])) {
94
            $val = $default;
95
        }
96
97
        return $val;
98
    }
99
100
    /**
101
     * [setCapacity Set the buckets capacity]
102
     * @param array $options
103
     */
104
    protected function setCapacity($options)
105
    {
106
        $hasCapacityOption = $this->hasOptionProperty($options, 'rateLimit');
107
108
        if (!is_numeric($hasCapacityOption) || empty($hasCapacityOption)) {
109
            $hasCapacityOption = false;
110
        }
111
112
        $this->capacity = ($hasCapacityOption) ? intval($hasCapacityOption) : intval($this->capacity);
113
    }
114
115
    /**
116
     * [getCapacity Get the buckets capacity]
117
     * @return integer
118
     */
119
    public function getCapacity()
120
    {
121
        return $this->capacity;
122
    }
123
124
    /**
125
     * [setTimeframe Set the window timeframe]
126
     * @param array $options
127
     */
128
    protected function setTimeframe($options)
129
    {
130
        $timeframe = $this->hasOptionProperty($options, 'rateWindow');
131
132
        if (is_string($timeframe)) {
133
            if (isset(self::$timeframe[$timeframe])) {
134
                $this->window = intval(self::$timeframe[$timeframe]);
135
                return;
136
            }
137
        }
138
139
        if (is_numeric($timeframe)) {
140
            if ($timeframe < 0) {
141
                $timeframe = ($timeframe*-1);
142
            }
143
            self::$timeframe['CUSTOM'] = $timeframe;
144
            $this->window = intval(self::$timeframe['CUSTOM']);
145
            return;
146
        }
147
148
        $this->window = self::$timeframe['MINUTE'];
149
    }
150
151
    /**
152
     * [getTimeframe Get the timeframe window]
153
     * @return integer|string
154
     */
155
    public function getTimeframe()
156
    {
157
        return $this->window;
158
    }
159
160
    /**
161
     * [setLeakRate Set the buckets leak rate]
162
     * Options: slow, medium, normal, default, fast or custom positive integer
163
     * @param array $options
164
     */
165
    protected function setLeakRate($options)
166
    {
167
        if (isset($options['leak']) && !$options['leak']) {
168
            $options['leakRate'] = 'default';
169
        }
170
171
        $leakRate = $this->hasOptionProperty($options, 'leakRate');
172
173
        if (empty($leakRate) || !is_string($leakRate)) {
174
            $leakRate = 'default';
175
        }
176
177
        $this->leakRate = $leakRate;
178
    }
179
180
    /**
181
     * [getLeakRate Get the buckets leak rate]
182
     * @return string|integer
183
     */
184
    public function getLeakRate()
185
    {
186
        return $this->leakRate;
187
    }
188
189
    /**
190
     * [setUnlimited Rate limiter bypass]
191
     * @param array $options
192
     */
193
    protected function setUnlimited($options)
194
    {
195
        $unlimited = false;
196
197
        if (isset($options['unlimited']) && ($options['unlimited'] == 1 || $options['unlimited'] == true)) {
198
            $unlimited = true;
199
        }
200
201
        $this->unlimited = $unlimited;
202
    }
203
204
    /**
205
     * [setUnlimited Rate limiter bypass in debug mode]
206
     * @param array $options
207
     */
208
    protected function setDebugMode($options)
209
    {
210
        $unlimited = false;
211
212
        if (isset($options['requestType']) && $options['requestType'] === 'debug') {
213
            $unlimited = true;
214
        }
215
216
        $this->unlimited = $unlimited;
217
    }
218
219
    /**
220
     * [isUnlimited Check if the Responsible API is set to unlimited]
221
     * @return boolean
222
     */
223
    protected function isUnlimited()
224
    {
225
        return $this->unlimited;
226
    }
227
}
228