Test Failed
Push — master ( 0bbeee...5fd70b )
by Hirofumi
02:10
created

JobConsume::createConsumer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
cc 1
eloc 4
nc 1
nop 0
crap 1.008
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 Interop\Queue\PsrContext;
8
use LogicException;
9
use Psr\Log\LoggerAwareTrait;
10
use Psr\Log\LoggerInterface;
11
use Psr\Log\NullLogger;
12
use Shippinno\Job\Application\Messaging\ConsumeStoredJobService;
13
use Shippinno\Job\Domain\Model\JobRunnerNotRegisteredException;
14
15
class JobConsume extends Command
16
{
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
     * @var ManagerRegistry|null
31
     */
32
    private $managerRegistry;
33
34
    /**
35
     * @param ConsumeStoredJobService $consumeStoredJobService
36
     * @param ManagerRegistry|null $managerRegistry
37
     * @param LoggerInterface|null $logger
38
     */
39 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...
40
        ConsumeStoredJobService $consumeStoredJobService,
41
        ManagerRegistry $managerRegistry = null,
42
        LoggerInterface $logger = null
43
    ) {
44
        parent::__construct();
45
        $this->consumeStoredJobService = $consumeStoredJobService;
46
        $this->managerRegistry = $managerRegistry;
47
        $this->setLogger(null !== $logger ? $logger : new NullLogger);
48 3
    }
49
50
    public function handle()
51
    {
52
        $queueName = env('JOB_CONSUME_QUEUE');
53
        if (!$queueName) {
54 3
            throw new LogicException('The env JOB_CONSUME_QUEUE is not defined');
55 3
        }
56 3
        $forever = !env('JOB_TESTING', false);
57 3
        do {
58 3
            if (null !== $this->managerRegistry) {
59 3
                $this->managerRegistry->getManager()->clear();
60
            }
61
            $this->consumeStoredJobService->execute($queueName);
62
            if (null !== $this->managerRegistry) {
63
                $this->managerRegistry->getManager()->flush();
64
            }
65
        } while ($forever);
66
    }
67
}
68