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

ExecuteJob::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4286
ccs 10
cts 10
cp 1
cc 3
eloc 9
nc 4
nop 2
crap 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