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

src/Service/QueueRequeueService.php (1 issue)

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\Entity\QueueEntityInterface;
9
use Lamoda\QueueBundle\Factory\PublisherFactory;
10
use Psr\Log\LoggerInterface;
11
use Throwable;
12
13
class QueueRequeueService
14
{
15
    /** @var PublisherFactory */
16
    protected $publisherFactory;
17
18
    /** @var QueueService */
19
    protected $queueService;
20
21
    /** @var LoggerInterface */
22
    protected $logger;
23
24 3
    public function __construct(
25
        PublisherFactory $publisherFactory,
26
        QueueService $queueService,
27
        LoggerInterface $logger
28
    ) {
29 3
        $this->publisherFactory = $publisherFactory;
30 3
        $this->queueService = $queueService;
31 3
        $this->logger = $logger;
32 3
    }
33
34 2
    public function restoreQueues(int $batchSize): bool
35
    {
36 2
        $this->queueService->beginTransaction();
37
38
        try {
39
            do {
40 2
                $queues = $this->queueService->getToRestore($batchSize);
41 1
                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 1
                    foreach ($queues as $queue) {
43 1
                        $this->publisherFactory->requeue($queue);
44 1
                        $this->queueService->flush($queue);
45
                    }
46
                }
47 1
                $this->queueService->commit();
48 1
                $this->publisherFactory->releaseAll();
49 1
            } while (count($queues) === $batchSize);
50 1
        } catch (Throwable $exception) {
51 1
            $this->queueService->rollback();
52
53 1
            $this->logger->error(
54 1
                ConstantMessage::QUEUE_CAN_NOT_REQUEUE,
55
                [
56 1
                    'exception' => get_class($exception),
57 1
                    'message' => $exception->getMessage(),
58
                ]
59
            );
60
61 1
            return false;
62
        }
63
64 1
        return true;
65
    }
66
67 1
    public function requeue(QueueEntityInterface $queue): void
68
    {
69 1
        $this->publisherFactory->requeue($queue);
70 1
        $this->publisherFactory->releaseAll();
71 1
    }
72
}
73