Passed
Push — master ( e73242...f49b80 )
by Hirofumi
02:35
created

JobEnqueue   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 0
loc 63
ccs 0
cts 37
cp 0
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
C handle() 0 27 7
1
<?php
2
3
namespace Shippinno\Job\Infrastructure\Ui\Console\Laravel\Command;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\ORM\EntityManager;
7
use Illuminate\Console\Command;
8
use LogicException;
9
use Psr\Log\LoggerAwareTrait;
10
use Psr\Log\LoggerInterface;
11
use Psr\Log\NullLogger;
12
use Shippinno\Job\Application\Messaging\EnqueueStoredJobsService;
13
use Shippinno\Job\Domain\Model\FailedToEnqueueStoredJobException;
14
15
class JobEnqueue extends Command
16
{
17
    use LoggerAwareTrait;
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $signature = 'job:enqueue';
23
24
    /**
25
     * @var EnqueueStoredJobsService
26
     */
27
    private $service;
28
29
    /**
30
     * @var ManagerRegistry|null
31
     */
32
    private $managerRegistry;
33
34
    /**
35
     * @param EnqueueStoredJobsService $service
36
     * @param ManagerRegistry|null $managerRegistry
37
     * @param LoggerInterface|null $logger
38
     */
39
    public function __construct(
40
        EnqueueStoredJobsService $service,
41
        ManagerRegistry $managerRegistry = null,
42
        LoggerInterface $logger = null
43
    ) {
44
        parent::__construct();
45
        $this->service = $service;
46
        $this->managerRegistry = $managerRegistry;
47
        $this->setLogger(null !== $logger ? $logger : new NullLogger);
48
    }
49
50
    public function handle()
51
    {
52
        $topic = env('JOB_ENQUEUE_TOPIC');
53
        if (!$topic) {
54
            throw new LogicException('The env JOB_ENQUEUE_TOPIC is not defined');
55
        }
56
        while (true) {
57
            if (null !== $this->managerRegistry) {
58
                $this->managerRegistry->getManager()->clear();
59
            }
60
            try {
61
                $enqueuedMessagesCount = $this->service->execute($topic);
62
                if ($enqueuedMessagesCount > 0) {
63
                    $this->logger->debug($enqueuedMessagesCount.' jobs enqueued.');
64
                    if (null !== $this->managerRegistry) {
65
                        $this->entityManager->flush();
0 ignored issues
show
Bug introduced by
The property entityManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
66
                    }
67
                }
68
            } catch (FailedToEnqueueStoredJobException $e) {
69
                $this->logger->alert('Failed to enqueue stored job, retrying in 60 seconds.', [
70
                    'exception' => $e,
71
                ]);
72
                sleep(60);
73
                continue;
74
            }
75
        }
76
    }
77
}