Passed
Push — v2 ( c4de3c...f057b3 )
by Brice
03:43
created

Consume::execute()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.0592

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 13
cts 15
cp 0.8667
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 4
nop 2
crap 5.0592
1
<?php
2
3
namespace JobQueue\Application\Console;
4
5
use JobQueue\Application\Utils\CommandTrait;
6
use JobQueue\Domain\Task\Profile;
7
use JobQueue\Domain\Worker\Worker;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, JobQueue\Application\Console\Worker. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use JobQueue\Infrastructure\ServiceContainer;
9
use Ramsey\Uuid\Uuid;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
final class Consume extends Command
17
{
18
    use CommandTrait;
19
20
    public function configure()
21
    {
22
        $this
23
            ->setName('consume')
24
            ->setDescription('Consumes tasks from the queue')
25
            ->setHelp("Consumes tasks from a queue depending on one or multiple profiles.  \nEach task is consumed by executing the corresponding job.\n ")
26
            ->addArgument('profile', InputArgument::REQUIRED, 'Name of the profile to consume')
27
            ->addOption('name', 'w', InputOption::VALUE_OPTIONAL, 'Name of the worker')
28
            ->addOption('quantity', 'x', InputOption::VALUE_OPTIONAL, 'Quantity of tasks to consume')
29
        ;
30
    }
31
32
    /**
33
     *
34
     * @param InputInterface $input
35
     * @param OutputInterface $output
36
     * @return int
37
     */
38 2
    protected function execute(InputInterface $input, OutputInterface $output): int
39
    {
40 2
        $services = ServiceContainer::getInstance();
41
42 2
        $worker = new Worker(
43 2
            $name = $input->getOption('name') ?: (string) Uuid::uuid4(),
44 2
            $services->queue,
45 2
            $profile = new Profile($input->getArgument('profile')),
46 2
            $services->dispatcher
47
        );
48
49 2
        if (isset($services->logger)) {
50
            $worker->setLogger($services->logger);
51
        }
52
53 2
        $this->formatInfoSection(sprintf('Worker %s handles "%s" tasks...', $name, $profile), $output);
54
55 2
        $worker->consume($quantity = (int) $input->getOption('quantity') ?: null);
56
57 2
        if ($quantity > 0) {
58 2
            $this->formatInfoSection(sprintf('Worker %s is done.', $name), $output);
59
        } else {
60
            $this->formatErrorSection(sprintf('Worker %s has hanged out!', $name), $output);
61
        }
62
63 2
        return 0;
64
    }
65
}
66