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

JobEnqueue   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 12.2 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 97.06%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 6
dl 10
loc 82
ccs 33
cts 34
cp 0.9706
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 2
B handle() 0 23 4
A topicName() 0 9 2
A flushAndLog() 0 8 2

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
     * @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