BlackListProcessor   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 9
Bugs 0 Features 1
Metric Value
wmc 8
c 9
b 0
f 1
lcom 1
cbo 3
dl 0
loc 55
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isPassed() 0 4 1
A isBanned() 0 4 1
A isBannedOnRateExceed() 0 4 1
A init() 0 12 2
A ban() 0 5 1
A banOnRateExceed() 0 5 1
A setStorage() 0 5 1
1
<?php
2
3
namespace Sokil\FraudDetector\Processor;
4
5
use Sokil\FraudDetector\Storage\StorageInterface;
6
7
class BlackListProcessor extends AbstractProcessor
8
{
9
    /**
10
     * @var \Sokil\FraudDetector\Storage\StorageInterface
11
     */
12
    private $storage;
13
14
    private $banOnRateExceed = false;
15
16
    public function init()
17
    {
18
        $self = $this;
19
20
        // rate exceed event handler
21
        $this->detector->subscribe('checkFailed:requestRate', function() use($self) {
22
            // ban on rate exceed
23
            if($self->isBannedOnRateExceed()) {
24
                $self->ban();
25
            }
26
        });
27
    }
28
29
    public function isPassed()
30
    {
31
        return !$this->isBanned();
32
    }
33
34
    public function ban()
35
    {
36
        $this->storage->store($this->detector->getKey());
37
        return $this;
38
    }
39
40
    public function isBanned()
41
    {
42
        return $this->storage->isStored($this->detector->getKey());
43
    }
44
45
    public function banOnRateExceed()
46
    {
47
        $this->banOnRateExceed = true;
48
        return $this;
49
    }
50
51
    public function isBannedOnRateExceed()
52
    {
53
        return true === $this->banOnRateExceed;
54
    }
55
56
    public function setStorage(StorageInterface $storage)
57
    {
58
        $this->storage = $storage;
59
        return $this;
60
    }
61
}