FakeCollector::isRateLimitExceed()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
1
<?php
2
3
namespace Sokil\FraudDetector\Collector;
4
5
class FakeCollector extends AbstractCollector
6
{
7
    private $keyList = array();
8
9
    public function isRateLimitExceed()
10
    {
11
        $timeNow = microtime(true);
12
13
        // is key not exists
14
        if(!isset($this->keyList[$this->key])) {
15
            return false;
16
        }
17
18
        // is in time slot
19
        if($this->keyList[$this->key]['expired'] < $timeNow) {
20
            return false;
21
        }
22
23
        // is requests limit reached
24
        return $this->keyList[$this->key]['requestNum'] >= $this->requestNum;
25
    }
26
27
    public function collect()
28
    {
29
        $timeNow = microtime(true);
30
31
        if(isset($this->keyList[$this->key]) && $this->keyList[$this->key]['expired'] > $timeNow) {
32
            $this->keyList[$this->key]['requestNum']++;
33
        } else {
34
            $this->keyList[$this->key] = array('expired' => $timeNow + $this->timeInterval, 'requestNum' => 1);
35
        }
36
    }
37
}