Completed
Pull Request — master (#47)
by Wachter
07:18
created

RunHandlerCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of php-task library.
5
 *
6
 * (c) php-task
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Task\TaskBundle\Command;
13
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Task\Handler\TaskHandlerFactoryInterface;
19
20
/**
21
 * Run pending tasks.
22
 */
23
class RunHandlerCommand extends Command
24
{
25
    /**
26
     * @var TaskHandlerFactoryInterface
27
     */
28
    private $handlerFactory;
29
30
    /**
31
     * @param string $name
32
     * @param TaskHandlerFactoryInterface $handlerFactory
33
     */
34
    public function __construct($name, TaskHandlerFactoryInterface $handlerFactory)
35
    {
36
        parent::__construct($name);
37
38
        $this->handlerFactory = $handlerFactory;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function configure()
45
    {
46
        $this
47
            ->setDescription('Run handler')
48
            ->setHelp(<<<'EOT'
49
The <info>%command.name%</info> command run given handler.
50
51
    $ %command.full_name% AppBundle\\Handler\\ImageHandler ./img/test-image.jpg
52
EOT
53
            )
54
        ->addArgument('handlerClass', InputArgument::REQUIRED, 'Handler which will be called')
55
        ->addArgument('workload', InputArgument::OPTIONAL, 'This will be passed to the handler');
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        $handlerClass = $input->getArgument('handlerClass');
64
        $workload = $input->getArgument('workload');
65
66
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
67
            $output->writeln(sprintf('Run command "%s" with workload "%s"', $handlerClass, $workload));
68
        }
69
70
        $result = $this->handlerFactory->create($handlerClass)->handle($workload);
0 ignored issues
show
Bug introduced by
It seems like $handlerClass defined by $input->getArgument('handlerClass') on line 63 can also be of type array<integer,string> or null; however, Task\Handler\TaskHandlerFactoryInterface::create() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $workload defined by $input->getArgument('workload') on line 64 can also be of type array<integer,string> or null; however, Task\Handler\TaskHandlerInterface::handle() does only seem to accept string|object<Serializable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
71
72
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
73
            $output->writeln(sprintf('Result: %s', json_encode($result)));
74
        }
75
    }
76
}
77