Passed
Push — master ( a9ce00...7fc17a )
by Timo
04:19
created

RequestLimitRuleset::getRequestLimitGroup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace hamburgscleanest\GuzzleAdvancedThrottle;
4
5
use GuzzleHttp\Promise\PromiseInterface;
6
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters\ArrayAdapter;
7
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters\LaravelAdapter;
8
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Interfaces\CacheStrategy;
9
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Interfaces\StorageInterface;
10
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Strategies\Cache;
11
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Strategies\ForceCache;
12
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Strategies\NoCache;
13
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\HostNotDefinedException;
14
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownCacheStrategyException;
15
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownStorageAdapterException;
16
use hamburgscleanest\GuzzleAdvancedThrottle\Helpers\InterfaceHelper;
17
use Illuminate\Config\Repository;
18
use Psr\Http\Message\RequestInterface;
19
20
/**
21
 * Class RequestLimitRuleset
22
 * @package hamburgscleanest\GuzzleAdvancedThrottle
23
 */
24
class RequestLimitRuleset
25
{
26
27
    /** @var array */
28
    private const STORAGE_MAP = [
29
        'array'   => ArrayAdapter::class,
30
        'laravel' => LaravelAdapter::class
31
    ];
32
33
    /** @var array */
34
    private const CACHE_STRATEGIES = [
35
        'no-cache'    => NoCache::class,
36
        'cache'       => Cache::class,
37
        'force-cache' => ForceCache::class
38
    ];
39
40
    /** @var array */
41
    private $_rules;
42
43
    /** @var StorageInterface */
44
    private $_storage;
45
46
    /** @var CacheStrategy */
47
    private $_cacheStrategy;
48
49
    /** @var Repository */
50
    private $_config;
51
52
    /**
53
     * RequestLimitRuleset constructor.
54
     * @param array $rules
55
     * @param string $cacheStrategy
56
     * @param string|null $storageAdapter
57
     * @param Repository|null $config
58
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownCacheStrategyException
59
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownStorageAdapterException
60
     */
61 30
    public function __construct(array $rules, string $cacheStrategy = 'no-cache', string $storageAdapter = 'array', Repository $config = null)
62
    {
63 30
        $this->_rules = $rules;
64 30
        $this->_config = $config;
65 30
        $this->_setStorageAdapter($storageAdapter);
66 24
        $this->_setCacheStrategy($cacheStrategy);
67 22
    }
68
69
    /**
70
     * Sets internal storage adapter for this rule set.
71
     * @param string $adapterName
72
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownStorageAdapterException
73
     */
74 30
    private function _setStorageAdapter(string $adapterName) : void
75
    {
76 30
        $adapterClassName = self::STORAGE_MAP[$adapterName] ?? $adapterName;
77
78 30
        if (!InterfaceHelper::implementsInterface($adapterClassName, StorageInterface::class))
79
        {
80 2
            throw new UnknownStorageAdapterException($adapterClassName, \array_values(self::STORAGE_MAP));
81
        }
82
83 28
        $this->_storage = new $adapterClassName($this->_config);
84 24
    }
85
86
    /**
87
     * Sets the caching strategy for this rule set.
88
     * @param string $cacheStrategy
89
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownCacheStrategyException
90
     */
91 24
    private function _setCacheStrategy(string $cacheStrategy) : void
92
    {
93 24
        $cacheStrategyClassName = self::CACHE_STRATEGIES[$cacheStrategy] ?? $cacheStrategy;
94
95 24
        if (!InterfaceHelper::implementsInterface($cacheStrategyClassName, CacheStrategy::class))
96
        {
97 2
            throw new UnknownCacheStrategyException($cacheStrategyClassName, \array_values(self::CACHE_STRATEGIES));
98
        }
99
100 22
        $this->_cacheStrategy = new $cacheStrategyClassName($this->_storage);
101 22
    }
102
103
    /**
104
     * @param array $rules
105
     * @param string $cacheStrategy
106
     * @param string $storageAdapter
107
     * @return static
108
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownStorageAdapterException
109
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownCacheStrategyException
110
     */
111 10
    public static function create(array $rules, string $cacheStrategy = 'no-cache', string $storageAdapter = 'array')
112
    {
113 10
        return new static($rules, $cacheStrategy, $storageAdapter);
114
    }
115
116
    /**
117
     * @param RequestInterface $request
118
     * @param callable $handler
119
     * @return PromiseInterface
120
     */
121 16
    public function cache(RequestInterface $request, callable $handler) : PromiseInterface
122
    {
123 16
        return $this->_cacheStrategy->request($request, $handler);
124
    }
125
126
    /**
127
     * @return RequestLimitGroup
128
     * @throws \Exception
129
     * @throws HostNotDefinedException
130
     */
131 18
    public function getRequestLimitGroup() : RequestLimitGroup
132
    {
133 18
        $requestLimitGroup = new RequestLimitGroup();
134 18
        foreach ($this->_rules as $host => $rules)
135
        {
136 18
            if (!\is_string($host))
137
            {
138 1
                throw new HostNotDefinedException();
139
            }
140
141 17
            $requestLimitGroup->addRules($host, $rules, $this->_storage);
142
        }
143
144 17
        return $requestLimitGroup;
145
    }
146
}