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

JobConsume::handle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.4285
c 3
b 0
f 0
cc 3
eloc 10
nc 2
nop 0
crap 3
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\ConsumeStoredJobService;
12
use Shippinno\Job\Infrastructure\Persistence\Doctrine\ManagerRegistryAwareTrait;
13
14
class JobConsume extends Command
15
{
16
    use ManagerRegistryAwareTrait;
17
    use LoggerAwareTrait;
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $signature = 'job:consume';
23
24
    /**
25
     * @var ConsumeStoredJobService
26
     */
27
    private $consumeStoredJobService;
28
29
    /**
30
     * @param ConsumeStoredJobService $consumeStoredJobService
31
     * @param ManagerRegistry|null $managerRegistry
32
     * @param LoggerInterface|null $logger
33
     */
34 2 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...
35
        ConsumeStoredJobService $consumeStoredJobService,
36
        ManagerRegistry $managerRegistry = null,
37
        LoggerInterface $logger = null
38
    ) {
39 2
        parent::__construct();
40 2
        $this->consumeStoredJobService = $consumeStoredJobService;
41 2
        $this->setManagerRegistry($managerRegistry);
0 ignored issues
show
Bug introduced by
It seems like $managerRegistry defined by parameter $managerRegistry on line 36 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...
42 2
        $this->setLogger(null !== $logger ? $logger : new NullLogger);
43 2
    }
44
45 2
    public function handle()
46
    {
47 2
        $queueName = env('JOB_CONSUME_QUEUE');
48 2
        if (!$queueName) {
49 1
            throw new LogicException('The env JOB_CONSUME_QUEUE is not defined');
50
        }
51 1
        $forever = !env('JOB_TESTING', false);
52
        do {
53 1
            $this->clear();
54 1
            $this->consumeStoredJobService->execute($queueName);
55 1
            $this->flush();
56 1
        } while ($forever);
57 1
    }
58
}
59