Completed
Push — master ( 451f62...fe985e )
by Anton
06:19
created

src/Service/QueueRepublishService.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 4
    public function __construct(
24
        PublisherFactory $publisherFactory,
25
        QueueService $queueService,
26
        LoggerInterface $logger
27
    ) {
28 4
        $this->publisherFactory = $publisherFactory;
29 4
        $this->queueService = $queueService;
30 4
        $this->logger = $logger;
31 4
    }
32
33 4
    public function republishQueues(int $batchSize): bool
34
    {
35 4
        $republishedQueueIds = [];
36
        do {
37 4
            $this->queueService->beginTransaction();
38
39
            try {
40 4
                $queues = $this->queueService->getToRepublish($batchSize);
41 3
                if ($queues) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $queues 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...
42 3
                    foreach ($queues as $queue) {
43 3
                        $this->publisherFactory->republish($queue);
44 3
                        $republishedQueueIds[] = $queue->getId();
45
                    }
46
                }
47
48 3
                $this->queueService->flush();
49 3
                $this->queueService->commit();
50 3
                $this->publisherFactory->releaseAll();
51 2
            } catch (Throwable $exception) {
52 2
                if ($this->queueService->isTransactionActive()) {
53 1
                    $this->queueService->rollback();
54
                }
55
56 2
                $this->logger->error(
57 2
                    ConstantMessage::QUEUE_CAN_NOT_REPUBLISH,
58
                    [
59 2
                        'exception' => get_class($exception),
60 2
                        'message' => $exception->getMessage(),
61
                    ]
62
                );
63
64 2
                return false;
65
            }
66 2
        } while (count($queues) === $batchSize);
67
68 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...
69 2
            $this->logger->info(
70 2
                ConstantMessage::QUEUE_SUCCESS_REPUBLISH,
71 2
                ['queuesIds' => implode(', ', $republishedQueueIds)]
72
            );
73
        }
74
75 2
        return true;
76
    }
77
}
78