GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Throttle::rate()   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
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Security\Protections;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Kernel\Http\Message\Request;
19
use O2System\Security\Protections\Throttle\Consumer;
20
use O2System\Security\Protections\Throttle\Rate;
21
22
/**
23
 * Class Throttle
24
 * @package O2System\Security\Protections
25
 */
26
class Throttle
27
{
28
    protected $key;
29
    protected $repository;
30
    protected $rate;
31
    protected $consumer;
32
33
    public function __construct()
34
    {
35
        $this->repository = new Throttle\Repository();
36
    }
37
38
    public function setKey($key)
39
    {
40
        $this->key = $key;
41
    }
42
43
    public function getConsumerData($consumerId)
44
    {
45
        if (class_exists('\O2System\Framework', false) or class_exists('\O2System\Reactor', false)) {
46
            return cache()->get('throttle-' . $consumerId);
0 ignored issues
show
Bug introduced by
The function cache was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
            return /** @scrutinizer ignore-call */ cache()->get('throttle-' . $consumerId);
Loading history...
47
        }
48
    }
49
50
    public function rate(array $config = [])
51
    {
52
        $this->rate = new Rate($config);
53
    }
54
55
    public function request(Request $request)
56
    {
57
        $this->consumer = new Consumer([
58
            'ipAddress' => $request->getClientIpAddress(),
59
            'userAgent' => $request->getClientUserAgent(),
60
        ]);
61
62
        $consumerId = empty($this->key) ? '' : $this->key . '.';
63
        $consumerId .= $this->consumer->getId();
64
65
        if ($this->repository->has($consumerId)) {
66
            $consumerData = $this->repository->get($consumerId);
67
            $consumerData[ 'id' ] = $consumerId;
68
            $consumerData[ 'currentCallTime' ] = $request->getTime();
69
            $consumerData[ 'attempts' ] = 1;
70
71
            $this->consumer->merge($consumerData);
0 ignored issues
show
Bug introduced by
It seems like $consumerData can also be of type false and null; however, parameter $values of O2System\Spl\DataStructu...SplArrayObject::merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
            $this->consumer->merge(/** @scrutinizer ignore-type */ $consumerData);
Loading history...
72
        } else {
73
            $consumerData = $this->consumer->getArrayCopy();
74
            $consumerData[ 'id' ] = $consumerId;
75
            $consumerData[ 'lastCallTime' ] = $consumerData[ 'currentCallTime' ] = $request->getTime();
76
            $consumerData[ 'attempts' ] = $consumerData[ 'attempts' ] + 1;
77
78
            $this->repository->store($consumerId, $consumerData);
79
        }
80
    }
81
82
    public function verify()
83
    {
84
        $currentTime = strtotime(date('r'));
85
        $timeCall = abs($this->consumer[ 'lastCallTime' ] - $currentTime);
86
87
        $this->consumer[ 'lastCallTime' ] = $currentTime;
88
89
        if ($timeCall > $this->rate[ 'span' ]) {
90
            return false;
91
        } elseif ($this->consumer[ 'attempts' ] > $this->rate[ 'attempts' ]) {
92
            return false;
93
        }
94
95
        return true;
96
    }
97
}