Completed
Pull Request — master (#3)
by Anton
04:32 queued 01:19
created

QueueRepublishService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.14%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 77
ccs 34
cts 35
cp 0.9714
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B republishQueues() 0 55 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\QueueBundle\Service;
6
7
use Lamoda\QueueBundle\ConstantMessage;
8
use Lamoda\QueueBundle\Factory\PublisherFactory;
9
use Psr\Log\LoggerInterface;
10
use Throwable;
11
12
class QueueRepublishService
13
{
14
    /** @var PublisherFactory */
15
    protected $publisherFactory;
16
17
    /** @var QueueService */
18
    protected $queueService;
19
20
    /** @var LoggerInterface */
21
    protected $logger;
22
23 3
    public function __construct(
24
        PublisherFactory $publisherFactory,
25
        QueueService $queueService,
26
        LoggerInterface $logger
27
    ) {
28 3
        $this->publisherFactory = $publisherFactory;
29 3
        $this->queueService = $queueService;
30 3
        $this->logger = $logger;
31 3
    }
32
33 3
    public function republishQueues(int $batchSize): bool
34
    {
35 3
        $i = 0;
36
37 3
        while (true) {
38 3
            $this->queueService->beginTransaction();
39
40
            try {
41 3
                $queues = $this->queueService->getToRepublish($batchSize);
42
43 2
                if (0 !== $i && count($queues) < $batchSize) {
44 1
                    $this->queueService->commit();
45
46 1
                    return true;
47
                }
48
49 2
                $republishedQueueIds = [];
50
51 2
                foreach ($queues as $queue) {
52 2
                    $this->publisherFactory->republish($queue);
53 2
                    $this->queueService->flush($queue);
54 2
                    $republishedQueueIds[] = $queue->getId();
55
                }
56
57 2
                $this->queueService->commit();
58 2
                $this->publisherFactory->releaseAll();
59
60 2
                if ($republishedQueueIds) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $republishedQueueIds of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
61 2
                    $this->logger->info(
62 2
                        ConstantMessage::QUEUE_SUCCESS_REPUBLISH,
63 2
                        ['queuesIds' => implode(', ', $republishedQueueIds)]
64
                    );
65
                }
66 1
            } catch (Throwable $exception) {
67 1
                $this->queueService->rollback();
68
69 1
                $this->logger->error(
70 1
                    ConstantMessage::QUEUE_CAN_NOT_REPUBLISH,
71
                    [
72 1
                        'exception' => get_class($exception),
73 1
                        'message' => $exception->getMessage(),
74
                    ]
75
                );
76
77 1
                return false;
78
            }
79
80 2
            if (0 === $i && count($queues) < $batchSize) {
81 1
                return true;
82
            }
83 1
            ++$i;
84
        }
85
86
        return true;
87
    }
88
}
89