RequestRestrictor::restrictRequestByIp()   A
last analyzed

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 1
dl 0
loc 3
rs 10
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