RequestRestrictor   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A restrictRequestByUserId() 0 3 1
A __construct() 0 3 1
A restrictRequestByIp() 0 3 1
A restrictByKey() 0 8 2
1
<?php
2
3
namespace NV\RequestLimitBundle\Utils;
4
5
use NV\RequestLimitBundle\Exception\RequestLimitReachedException;
6
use NV\RequestLimitBundle\Storage\StorageManager;
7
8
class RequestRestrictor
9
{
10
    /**
11
     * @var StorageManager
12
     */
13
    private $storageManager;
14
15
    /**
16
     * @param StorageManager $storageManager
17
     */
18
    public function __construct(StorageManager $storageManager)
19
    {
20
        $this->storageManager = $storageManager;
21
    }
22
23
    /**
24
     * @param $userIp
25
     * @return mixed
26
     */
27
    public function restrictRequestByIp($userIp)
28
    {
29
        return $this->restrictByKey($userIp);
30
    }
31
32
    /**
33
     * @param $userId
34
     * @return mixed
35
     */
36
    public function restrictRequestByUserId($userId)
37
    {
38
       return $this->restrictByKey($userId);
39
    }
40
41
    /**
42
     * @param $key
43
     * @return mixed
44
     * @throws RequestLimitReachedException
45
     */
46
    private function restrictByKey($key) {
47
        if ($this->storageManager->hasItem($key)) {
48
            throw new RequestLimitReachedException();
49
        }
50
51
        $this->storageManager->setItem($key);
52
53
        return $this->storageManager->getItem($key);
54
    }
55
}
56