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

JobConsume::terminate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
use Throwable;
14
15
class JobConsume extends Command
16
{
17
    use ManagerRegistryAwareTrait;
18
    use LoggerAwareTrait;
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected $signature = 'job:consume';
24
25
    /**
26
     * @var ConsumeStoredJobService
27
     */
28
    private $consumeStoredJobService;
29
30
    /**
31
     * @var bool
32
     */
33
    private $terminating = false;
34
35
    /**
36
     * @param ConsumeStoredJobService $consumeStoredJobService
37
     * @param ManagerRegistry|null $managerRegistry
38
     * @param LoggerInterface|null $logger
39
     */
40 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...
41
        ConsumeStoredJobService $consumeStoredJobService,
42
        ManagerRegistry $managerRegistry = null,
43
        LoggerInterface $logger = null
44
    ) {
45 2
        parent::__construct();
46 2
        $this->consumeStoredJobService = $consumeStoredJobService;
47 2
        $this->setManagerRegistry($managerRegistry);
48 2
        $this->setLogger(null !== $logger ? $logger : new NullLogger);
49 2
    }
50
51 2
    public function handle()
52
    {
53 2 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 2
            pcntl_async_signals(true);
55 2
            pcntl_signal(SIGTERM, [$this, 'terminate']);
56 2
            pcntl_signal(SIGINT, [$this, 'terminate']);
57
        }
58
59 2
        $queueName = $this->queueName();
60 1
        $forever = !env('JOB_TESTING', false);
61
        do {
62 1
            $this->clear();
63 1
            $this->consumeStoredJobService->execute(
64 1
                $queueName,
65
                function () {
66
                    try {
67
                        $this->flush();
68
                    } catch (Throwable $e) {
69
                        return false;
70
                    }
71
                    return true;
72 1
                },
73
                function () {
74
                    $this->clear();
75 1
                }
76
            );
77
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
78 1
        } while ($forever && !$this->terminating);
79 1
    }
80
81
    /**
82
     * @return string
83
     */
84 2
    private function queueName(): string
85
    {
86 2
        $queueName = env('JOB_CONSUME_QUEUE');
87 2
        if (!$queueName) {
88 1
            throw new LogicException('The env JOB_CONSUME_QUEUE is not defined');
89
        }
90
91 1
        return $queueName;
92
    }
93
94
    public function terminate()
95
    {
96
        $this->logger->info('Terminating job:consume.');
97
        $this->terminating = true;
98
    }
99
}
100