Completed
Push — master ( e02e4d...5773dd )
by
unknown
02:54 queued 11s
created

DelayStrategyResolver::getDefaultStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Lamoda\QueueBundle\Service;
5
6
use Lamoda\QueueBundle\Exception\UnknownStrategyKeyException;
7
use Lamoda\QueueBundle\Strategy\Delay\DelayStrategyInterface;
8
9
class DelayStrategyResolver
10
{
11
    public const DEFAULT_STRATEGY = 'default_delay_strategy';
12
13
    /**
14
     * @var array
15
     */
16
    protected $handlers;
17
18
    /**
19
     * @var array
20
     */
21
    protected $queuesConfiguration;
22
23 3
    public function __construct(iterable $handlers, array $queuesConfiguration = [])
24
    {
25 3
        $this->handlers            = iterator_to_array($handlers);
26 3
        $this->queuesConfiguration = $queuesConfiguration;
27 3
    }
28
29
    /**
30
     * @param string $queueName
31
     *
32
     * @return DelayStrategyInterface
33
     * @throws UnknownStrategyKeyException
34
     */
35 5
    public function getStrategy(string $queueName): DelayStrategyInterface
36
    {
37 5
        $strategyKey = $this->queuesConfiguration[$queueName] ?? self::DEFAULT_STRATEGY;
38
39 5
        if (isset($this->handlers[$strategyKey])) {
40 3
            return $this->handlers[$strategyKey];
41
        }
42
43 2
        throw new UnknownStrategyKeyException(sprintf('Delay strategy with key: %s doesn\'t exist', $strategyKey));
44
    }
45
46 1
    public function getDefaultStrategy(): DelayStrategyInterface
47
    {
48 1
        return $this->handlers[self::DEFAULT_STRATEGY];
49
    }
50
}
51