Test Failed
Push — master ( bf1804...12692f )
by Hirofumi
03:59
created

JobEnqueue::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 10
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 8
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Shippinno\Job\Infrastructure\Ui\Console\Laravel\Command;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Illuminate\Console\Command;
7
use LogicException;
8
use Psr\Log\LoggerAwareTrait;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
use Shippinno\Job\Application\Messaging\EnqueueStoredJobsService;
12
use Shippinno\Job\Domain\Model\FailedToEnqueueStoredJobException;
13
use Shippinno\Job\Infrastructure\Persistence\Doctrine\ManagerRegistryAwareTrait;
14
15
class JobEnqueue extends Command
16
{
17
    use ManagerRegistryAwareTrait;
18
    use LoggerAwareTrait;
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected $signature = 'job:enqueue';
24
25
    /**
26
     * @var EnqueueStoredJobsService
27
     */
28
    private $service;
29
30
    /**
31
     * @param EnqueueStoredJobsService $service
32
     * @param ManagerRegistry|null $managerRegistry
33
     * @param LoggerInterface|null $logger
34
     */
35 1 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
36
        EnqueueStoredJobsService $service,
37
        ManagerRegistry $managerRegistry = null,
38
        LoggerInterface $logger = null
39
    ) {
40 1
        parent::__construct();
41 1
        $this->service = $service;
42 1
        $this->setManagerRegistry($managerRegistry);
43 1
        $this->setLogger(null !== $logger ? $logger : new NullLogger);
44 1
    }
45
46 1
    public function handle()
47
    {
48 1
        $topicName = $this->topicName();
49
        $testing = env('JOB_TESTING', false);
50
        do {
51
            $this->clear();
52
            try {
53
                $enqueuedMessagesCount = $this->service->execute($topicName);
54
                $this->flushAndLog(
55
                    $enqueuedMessagesCount > 0,
56
                    sprintf('%d job(s) enqueued.', $enqueuedMessagesCount)
57
                );
58
            } catch (FailedToEnqueueStoredJobException $e) {
59
                $enqueuedMessagesCount = $e->enqueuedMessagesCount();
60
                $this->flushAndLog(
61
                    $enqueuedMessagesCount > 0,
62
                    sprintf('%d job(s) enqueued.', $enqueuedMessagesCount)
63
                );
64
                $interval = !$testing ? 60 : 0;
65
                $this->logger->alert(
66
                    sprintf('Failed to enqueue stored job, retrying in %d second(s).', $interval),
67
                    ['exception' => $e]
68
                );
69
                sleep($interval);
70
                continue;
71
            }
72
        } while (!$testing);
73
    }
74
75
    /**
76
     * @return string
77
     */
78 1
    private function topicName(): string
79
    {
80 1
        $topicName = env('JOB_ENQUEUE_TOPIC');
81 1
        if (!$topicName) {
82 1
            throw new LogicException('The env JOB_ENQUEUE_TOPIC is not defined');
83
        }
84
85
        return $topicName;
86
    }
87
88
    /**
89
     * @param bool $condition
90
     * @param string $logMessage
91
     */
92
    private function flushAndLog(bool $condition, string $logMessage): void
93
    {
94
        if (!$condition) {
95
            return;
96
        }
97
        $this->flush();
98
        $this->logger->debug($logMessage);
99
    }
100
101
}
102