Passed
Pull Request — master (#10)
by
unknown
02:35
created

DelayService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 45
ccs 16
cts 18
cp 0.8889
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getStartDateTime() 0 4 1
A delayQueue() 0 21 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\QueueBundle\Service;
6
7
use DateTime;
8
use Lamoda\QueueBundle\Entity\QueueEntityInterface;
9
use Lamoda\QueueBundle\Exception\UnknownStrategyKeyException;
10
use Psr\Log\LoggerInterface;
11
12
class DelayService
13
{
14
    /**
15
     * @var DelayStrategyResolver
16
     */
17
    protected $strategyService;
18
19
    /**
20
     * @var LoggerInterface
21
     */
22
    private $logger;
23
24 2
    public function __construct(DelayStrategyResolver $strategyService, LoggerInterface $logger)
25
    {
26 2
        $this->strategyService = $strategyService;
27 2
        $this->logger          = $logger;
28 2
    }
29
30 2
    public function delayQueue(QueueEntityInterface $queue): QueueEntityInterface
31
    {
32
        try {
33 2
            $strategy = $this->strategyService->getStrategy($queue->getName());
34 1
        } catch (UnknownStrategyKeyException $exception) {
35 1
            $this->logger->warning(
36 1
                $exception->getMessage(), [
37 1
                    'queue_name' => $queue->getName(),
38
                ]
39
            );
40 1
            $strategy = $this->strategyService->getDefaultStrategy();
41
        }
42
43 2
        $iteration        = $queue->getAttempts() ?? 1;
44 2
        $newDelayInterval = $strategy->generateInterval($iteration);
45 2
        $delayUntil       = $this->getStartDateTime()->add($newDelayInterval);
46
47 2
        $queue->setWaiting($delayUntil);
48
49 2
        return $queue;
50
    }
51
52
    protected function getStartDateTime(): DateTime
53
    {
54
        return new DateTime();
55
    }
56
}
57