Passed
Push — master ( 99df08...45269c )
by Hirofumi
02:05
created

JobEnqueue::handle()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 26
ccs 19
cts 19
cp 1
rs 8.439
c 2
b 0
f 0
cc 6
eloc 20
nc 9
nop 0
crap 6
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);
0 ignored issues
show
Bug introduced by
It seems like $managerRegistry defined by parameter $managerRegistry on line 37 can be null; however, Shippinno\Job\Infrastruc...t::setManagerRegistry() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
43 3
        $this->setLogger(null !== $logger ? $logger : new NullLogger);
44 3
    }
45
46 3
    public function handle()
47
    {
48 3
        $topic = env('JOB_ENQUEUE_TOPIC');
49 3
        if (!$topic) {
50 1
            throw new LogicException('The env JOB_ENQUEUE_TOPIC is not defined');
51
        }
52 2
        $testing = env('JOB_TESTING', false);
53
        do {
54 2
            $this->clear();
55
            try {
56 2
                $enqueuedMessagesCount = $this->service->execute($topic);
57 1
                if ($enqueuedMessagesCount > 0) {
58 1
                    $this->flush();
59 1
                    $this->logger->debug(sprintf('%d jobs enqueued.', $enqueuedMessagesCount));
60
                }
61 1
            } catch (FailedToEnqueueStoredJobException $e) {
62 1
                $interval = !$testing ? 60 : 0;
63 1
                $this->logger->alert(
64 1
                    sprintf('Failed to enqueue stored job, retrying in %d second(s).', $interval),
65 1
                    ['exception' => $e,]
66
                );
67 1
                sleep($interval);
68 1
                continue;
69
            }
70 2
        } while (!$testing);
71 2
    }
72
}
73