Throttle   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
eloc 13
c 6
b 0
f 1
dl 0
loc 90
ccs 13
cts 13
cp 1
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 3 1
A get() 0 9 2
A getTransformer() 0 3 1
A getFactory() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Laravel Throttle.
7
 *
8
 * (c) Graham Campbell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace GrahamCampbell\Throttle;
15
16
use GrahamCampbell\Throttle\Factories\FactoryInterface;
17
use GrahamCampbell\Throttle\Transformers\TransformerFactoryInterface;
18
19
/**
20
 * This is the throttle class.
21
 *
22
 * @method bool attempt(array|\Illuminate\Http\Request $data, int $limit, int $time)
23
 * @method \GrahamCampbell\Throttle\Throttlers\ThrottlerInterface hit(array|\Illuminate\Http\Request $data, int $limit, int $time)
24
 * @method \GrahamCampbell\Throttle\Throttlers\ThrottlerInterface clear(array|\Illuminate\Http\Request $data, int $limit, int $time)
25
 * @method int count(array|\Illuminate\Http\Request $data, int $limit, int $time)
26
 * @method bool check(array|\Illuminate\Http\Request $data, int $limit, int $time)
27
 *
28
 * @author Graham Campbell <[email protected]>
29
 */
30
class Throttle
31
{
32
    /**
33
     * The cached throttler instances.
34
     *
35
     * @var \GrahamCampbell\Throttle\Throttlers\ThrottlerInterface[]
36
     */
37
    protected $throttlers = [];
38
39
    /**
40
     * The throttle factory instance.
41
     *
42
     * @var \GrahamCampbell\Throttle\Factories\FactoryInterface
43
     */
44
    protected $factory;
45
46
    /**
47
     * The transformer factory instance.
48
     *
49
     * @var \GrahamCampbell\Throttle\Transformers\TransformerFactoryInterface
50
     */
51
    protected $transformer;
52
53
    /**
54
     * Create a new instance.
55
     *
56
     * @param \GrahamCampbell\Throttle\Factories\FactoryInterface               $factory
57
     * @param \GrahamCampbell\Throttle\Transformers\TransformerFactoryInterface $transformer
58
     *
59
     * @return void
60
     */
61 144
    public function __construct(FactoryInterface $factory, TransformerFactoryInterface $transformer)
62
    {
63 144
        $this->factory = $factory;
64 144
        $this->transformer = $transformer;
65 144
    }
66
67
    /**
68
     * Get a new throttler.
69
     *
70
     * @param mixed $data
71
     * @param int   $limit
72
     * @param int   $time
73
     *
74
     * @return \GrahamCampbell\Throttle\Throttlers\ThrottlerInterface
75
     */
76 108
    public function get($data, int $limit = 10, int $time = 60)
77
    {
78 108
        $transformed = $this->transformer->make($data)->transform($data, $limit, $time);
79
80 108
        if (!array_key_exists($key = $transformed->getKey(), $this->throttlers)) {
81 108
            $this->throttlers[$key] = $this->factory->make($transformed);
82
        }
83
84 108
        return $this->throttlers[$key];
85
    }
86
87
    /**
88
     * Get the cache instance.
89
     *
90
     * @return \GrahamCampbell\Throttle\Factories\FactoryInterface
91
     */
92 36
    public function getFactory()
93
    {
94 36
        return $this->factory;
95
    }
96
97
    /**
98
     * Get the transformer factory instance.
99
     *
100
     * @codeCoverageIgnore
101
     *
102
     * @return \GrahamCampbell\Throttle\Transformers\TransformerFactoryInterface
103
     */
104
    public function getTransformer()
105
    {
106
        return $this->transformer;
107
    }
108
109
    /**
110
     * Dynamically pass methods to a new throttler instance.
111
     *
112
     * @param string $method
113
     * @param array  $parameters
114
     *
115
     * @return mixed
116
     */
117 84
    public function __call(string $method, array $parameters)
118
    {
119 84
        return $this->get(...$parameters)->$method();
120
    }
121
}
122