Passed
Push — master ( cd26ab...d2aa98 )
by Hirofumi
02:14
created

JobEnqueue::flushAndLog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2.0185
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 3 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 3
        parent::__construct();
41 3
        $this->service = $service;
42 3
        $this->setManagerRegistry($managerRegistry);
43 3
        $this->setLogger(null !== $logger ? $logger : new NullLogger);
44 3
    }
45
46 3
    public function handle()
47
    {
48 3
        $topicName = $this->topicName();
49 2
        $testing = env('JOB_TESTING', false);
50
        do {
51 2
            $this->clear();
52
            try {
53 2
                $enqueuedMessagesCount = $this->service->execute($topicName);
54 1
                $this->flushAndLog(
55 1
                    $enqueuedMessagesCount > 0,
56 1
                    sprintf('%d job(s) enqueued.', $enqueuedMessagesCount)
57
                );
58 1
            } catch (FailedToEnqueueStoredJobException $e) {
59 1
                $interval = !$testing ? 60 : 0;
60 1
                $this->logger->alert(
61 1
                    sprintf('Failed to enqueue stored job, retrying in %d second(s).', $interval),
62 1
                    ['exception' => $e]
63
                );
64 1
                sleep($interval);
65 1
                continue;
66
            }
67 2
        } while (!$testing);
68 2
    }
69
70
    /**
71
     * @return string
72
     */
73 3
    private function topicName(): string
74
    {
75 3
        $topicName = env('JOB_ENQUEUE_TOPIC');
76 3
        if (!$topicName) {
77 1
            throw new LogicException('The env JOB_ENQUEUE_TOPIC is not defined');
78
        }
79
80 2
        return $topicName;
81
    }
82
83
    /**
84
     * @param bool $condition
85
     * @param string $logMessage
86
     */
87 1
    private function flushAndLog(bool $condition, string $logMessage): void
88
    {
89 1
        if (!$condition) {
90
            return;
91
        }
92 1
        $this->flush();
93 1
        $this->logger->debug($logMessage);
94 1
    }
95
96
}
97