Completed
Push — master ( 553806...856339 )
by Krishnaprasad
02:34
created

RateLimiter::createThrottler()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
crap 3
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2015 Krishnaprasad MG <[email protected]>
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
namespace Sunspikes\Ratelimit;
27
28
use Sunspikes\Ratelimit\Throttle\Entity\Data;
29
use Sunspikes\Ratelimit\Throttle\Settings\ThrottleSettingsInterface;
30
use Sunspikes\Ratelimit\Throttle\Throttler\ThrottlerInterface;
31
use Sunspikes\Ratelimit\Throttle\Factory\FactoryInterface as ThrottlerFactoryInterface;
32
use Sunspikes\Ratelimit\Throttle\Hydrator\FactoryInterface as HydratorFactoryInterface;
33
34
class RateLimiter implements RateLimiterInterface
35
{
36
    /**
37
     * @var ThrottlerInterface[]
38
     */
39
    protected $throttlers;
40
41
    /**
42
     * @var ThrottlerFactoryInterface
43
     */
44
    protected $throttlerFactory;
45
46
    /**
47
     * @var HydratorFactoryInterface
48
     */
49
    protected $hydratorFactory;
50
51
    /**
52
     * @var ThrottleSettingsInterface
53
     */
54
    private $defaultSettings;
55
56
    /**
57
     * @param ThrottlerFactoryInterface $throttlerFactory
58
     * @param HydratorFactoryInterface  $hydratorFactory
59
     * @param ThrottleSettingsInterface $defaultSettings
60
     */
61 32
    public function __construct(
62
        ThrottlerFactoryInterface $throttlerFactory,
63
        HydratorFactoryInterface $hydratorFactory,
64
        ThrottleSettingsInterface $defaultSettings
65
    ) {
66 32
        $this->throttlerFactory = $throttlerFactory;
67 32
        $this->hydratorFactory = $hydratorFactory;
68 32
        $this->defaultSettings = $defaultSettings;
69 32
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 32
    public function get($data, ThrottleSettingsInterface $settings = null)
75
    {
76 32
        if (empty($data)) {
77 1
            throw new \InvalidArgumentException('Invalid data, please check the data.');
78
        }
79
80 31
        $object = $this->hydratorFactory->make($data)->hydrate($data);
81
82 31
        if (!isset($this->throttlers[$object->getKey()])) {
83 31
            $this->throttlers[$object->getKey()] = $this->createThrottler($object, $settings);
84 31
        }
85
86 31
        return $this->throttlers[$object->getKey()];
87
    }
88
89
    /**
90
     * @param Data                           $object
91
     * @param ThrottleSettingsInterface|null $settings
92
     *
93
     * @return ThrottlerInterface
94
     */
95 31
    private function createThrottler(Data $object, ThrottleSettingsInterface $settings = null)
96
    {
97 31
        if (null === $settings) {
98 29
            $settings = $this->defaultSettings;
99 29
        } else {
100
            try {
101 2
                $settings = $this->defaultSettings->merge($settings);
102 2
            } catch (\InvalidArgumentException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
103
            }
104
        }
105
106 31
        return $this->throttlerFactory->make($object, $settings);
107
    }
108
}
109