Test Failed
Push — master ( 05b8bb...23bfc5 )
by Hirofumi
03:06
created

JobEnqueue   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 4.85 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 89.13%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 5
loc 103
ccs 41
cts 46
cp 0.8913
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
B handle() 5 34 6
A topicName() 0 9 2
A flushAndLog() 0 8 2
A terminate() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     * @var bool
32
     */
33
    private $terminating = false;
34
35
    /**
36
     * @param EnqueueStoredJobsService $service
37
     * @param ManagerRegistry|null $managerRegistry
38
     * @param LoggerInterface|null $logger
39
     */
40 4
    public function __construct(
41
        EnqueueStoredJobsService $service,
42
        ManagerRegistry $managerRegistry = null,
43
        LoggerInterface $logger = null
44
    ) {
45 4
        parent::__construct();
46 4
        $this->service = $service;
47 4
        $this->setManagerRegistry($managerRegistry);
48 4
        $this->setLogger(null !== $logger ? $logger : new NullLogger);
49 4
    }
50
51 4
    public function handle()
52
    {
53 4 View Code Duplication
        if (extension_loaded('pcntl')) {
0 ignored issues
show
Duplication introduced by
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...
54 4
            pcntl_async_signals(true);
55 4
            pcntl_signal(SIGTERM, [$this, 'terminate']);
56 4
            pcntl_signal(SIGINT, [$this, 'terminate']);
57
        }
58
59 4
        $topicName = $this->topicName();
60 3
        $testing = env('JOB_TESTING', false);
61
        do {
62 3
            $this->clear();
63
            try {
64 3
                $enqueuedMessagesCount = $this->service->execute($topicName);
65 2
                $this->flushAndLog(
66 2
                    $enqueuedMessagesCount > 0,
67 2
                    sprintf('%d job(s) enqueued.', $enqueuedMessagesCount)
68
                );
69 1
            } catch (FailedToEnqueueStoredJobException $e) {
70 1
                $enqueuedMessagesCount = $e->enqueuedMessagesCount();
71 1
                $this->flushAndLog(
72 1
                    $enqueuedMessagesCount > 0,
73 1
                    sprintf('%d job(s) enqueued.', $enqueuedMessagesCount)
74
                );
75 1
                $interval = !$testing ? 60 : 0;
76 1
                $this->logger->alert(
77 1
                    sprintf('Failed to enqueue stored job, retrying in %d second(s).', $interval),
78 1
                    ['exception' => $e]
79
                );
80 1
                sleep($interval);
81 1
                continue;
82
            }
83 3
        } while (!$this->terminating && !$testing);
84 3
    }
85
86
    /**
87
     * @return string
88
     */
89 4
    private function topicName(): string
90
    {
91 4
        $topicName = env('JOB_ENQUEUE_TOPIC');
92 4
        if (!$topicName) {
93 1
            throw new LogicException('The env JOB_ENQUEUE_TOPIC is not defined');
94
        }
95
96 3
        return $topicName;
97
    }
98
99
    /**
100
     * @param bool $condition
101
     * @param string $logMessage
102
     */
103 3
    private function flushAndLog(bool $condition, string $logMessage): void
104
    {
105 3
        if (!$condition) {
106
            return;
107
        }
108 3
        $this->flush();
109 3
        $this->logger->debug($logMessage);
110 3
    }
111
112
    public function terminate()
113
    {
114
        $this->logger->info('Terminating job:enqueue.');
115
        $this->terminating = true;
116
    }
117
}
118