Completed
Push — master ( 49a196...e8b010 )
by
unknown
04:13
created

src/Service/QueueRepublishService.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\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 2
    public function __construct(
24
        PublisherFactory $publisherFactory,
25
        QueueService $queueService,
26
        LoggerInterface $logger
27
    ) {
28 2
        $this->publisherFactory = $publisherFactory;
29 2
        $this->queueService = $queueService;
30 2
        $this->logger = $logger;
31 2
    }
32
33 2
    public function republishQueues(int $batchSize): bool
34
    {
35 2
        $this->queueService->beginTransaction();
36
37
        try {
38 2
            $republishedQueueIds = [];
39 View Code Duplication
            do {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40 2
                $queues = $this->queueService->getToRepublish($batchSize);
41 1
                if ($queues) {
42 1
                    foreach ($queues as $queue) {
43 1
                        $this->publisherFactory->republish($queue);
44 1
                        $this->queueService->flush($queue);
45 1
                        $republishedQueueIds[] = $queue->getId();
46
                    }
47
                }
48 1
                $this->queueService->commit();
49 1
                $this->publisherFactory->releaseAll();
50 1
            } while (count($queues) === $batchSize);
51
52 1
            if ($republishedQueueIds) {
53 1
                $this->logger->info(
54 1
                    ConstantMessage::QUEUE_SUCCESS_REPUBLISH,
55 1
                    ['queuesIds' => implode(', ', $republishedQueueIds)]
56
                );
57
            }
58 1
        } catch (Throwable $exception) {
59 1
            $this->queueService->rollback();
60
61 1
            $this->logger->error(
62 1
                ConstantMessage::QUEUE_CAN_NOT_REPUBLISH,
63
                [
64 1
                    'exception' => get_class($exception),
65 1
                    'message' => $exception->getMessage(),
66
                ]
67
            );
68
69 1
            return false;
70
        }
71
72 1
        return true;
73
    }
74
}
75