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

ThrottlerFactory::createThrottler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 2
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\Throttle\Factory;
27
28
use Sunspikes\Ratelimit\Cache\Adapter\CacheAdapterInterface;
29
use Sunspikes\Ratelimit\Throttle\Entity\Data;
30
use Sunspikes\Ratelimit\Throttle\Settings\ElasticWindowSettings;
31
use Sunspikes\Ratelimit\Throttle\Settings\ThrottleSettingsInterface;
32
use Sunspikes\Ratelimit\Throttle\Throttler\ElasticWindowThrottler;
33
34
class ThrottlerFactory implements FactoryInterface
35
{
36
    /**
37
     * @var CacheAdapterInterface
38
     */
39
    protected $cacheAdapter;
40
41
    /**
42
     * @param CacheAdapterInterface $cacheAdapter
43
     */
44 37
    public function __construct(CacheAdapterInterface $cacheAdapter)
45
    {
46 37
        $this->cacheAdapter = $cacheAdapter;
47 37
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52 37
    public function make(Data $data, ThrottleSettingsInterface $settings)
53
    {
54 37
        if (!$settings->isValid()) {
55 2
            throw new \InvalidArgumentException('Provided throttler settings not valid');
56
        }
57
58 35
        return $this->createThrottler($data, $settings);
59
    }
60
61
    /**
62
     * @param Data                      $data
63
     * @param ThrottleSettingsInterface $settings
64
     *
65
     * @return ElasticWindowThrottler
66
     */
67 9
    protected function createThrottler(Data $data, ThrottleSettingsInterface $settings)
68
    {
69 9
        if ($settings instanceof ElasticWindowSettings) {
70 7
            return new ElasticWindowThrottler(
71 7
                $this->cacheAdapter,
72 7
                $data->getKey(),
73 7
                $settings->getLimit(),
74 7
                $settings->getTime()
75 7
            );
76
        }
77
78 2
        throw new \InvalidArgumentException(
79 2
            sprintf('Unable to create throttler for %s settings', get_class($settings))
80 2
        );
81
    }
82
}
83