Completed
Push — master ( 066d89...19607d )
by De
01:54
created

FakeCollector   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 6
c 7
b 2
f 0
lcom 1
cbo 1
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isRateLimitExceed() 0 17 3
A collect() 0 10 3
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
}