Completed
Push — master ( 2f7a99...5fa5de )
by Matze
10:42
created

ExecuteJob   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 45
rs 10
ccs 19
cts 19
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 7 1
A execute() 0 16 3
1
<?php
2
3
namespace BrainExe\Core\MessageQueue\Command;
4
5
use BrainExe\Core\MessageQueue\Job;
6
use BrainExe\Core\MessageQueue\Worker;
7
use Exception;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use BrainExe\Core\Annotations\Command as CommandAnnotation;
13
use BrainExe\Annotations\Annotations\Inject;
14
15
/**
16
 * @CommandAnnotation("MessageQueue.Command.ExecuteJob")
17
 */
18
class ExecuteJob extends Command
19
{
20
21
    /**
22
     * @Inject("@MessageQueue.Worker")
23
     * @param Worker $worker
24
     */
25 4
    public function __construct(Worker $worker)
26
    {
27 4
        $this->worker = $worker;
0 ignored issues
show
Bug introduced by
The property worker 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...
28
29 4
        parent::__construct(null);
30 4
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 4
    protected function configure()
36
    {
37
        $this
38 4
            ->setName('messagequeue:execute')
39 4
            ->setDescription('Runs message queue job')
40 4
            ->addArgument('job', InputArgument::REQUIRED);
41 4
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 2
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48 2
        $data = $input->getArgument('job');
49 2
        if (strpos($data, '#') !== false) {
50 2
            list (, $data) = explode('#', $data, 2);
51
        }
52
53 2
        $raw = @base64_decode($data);
54 2
        $job = @unserialize($raw);
55
56 2
        if (!$job instanceof Job) {
57 1
            throw new Exception(sprintf('Invalid job: %s', $input->getArgument('job')));
58
        }
59
60 1
        $this->worker->executeJob($job);
61 1
    }
62
}
63