Completed
Pull Request — master (#40)
by Wachter
05:27
created

ExecuteCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\ConsoleOutputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Task\Handler\TaskHandlerFactoryInterface;
20
use Task\Storage\TaskExecutionRepositoryInterface;
21
22
/**
23
 * Executes given execution identified by uuid.
24
 */
25
class ExecuteCommand extends Command
26
{
27
    const START_RESULT = '<?result';
28
    const END_RESULT = '?>';
29
30
    /**
31
     * @var TaskHandlerFactoryInterface
32
     */
33
    private $handlerFactory;
34
35
    /**
36
     * @var TaskExecutionRepositoryInterface
37
     */
38
    private $executionRepository;
39
40
    /**
41
     * @param string $name
42
     * @param TaskHandlerFactoryInterface $handlerFactory
43
     * @param TaskExecutionRepositoryInterface $executionRepository
44
     */
45 13
    public function __construct(
46
        $name,
47
        TaskHandlerFactoryInterface $handlerFactory,
48
        TaskExecutionRepositoryInterface $executionRepository
49
    ) {
50 13
        parent::__construct($name);
51
52 13
        $this->handlerFactory = $handlerFactory;
53 13
        $this->executionRepository = $executionRepository;
54 13
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 13
    protected function configure()
60
    {
61 13
        $this->addArgument('uuid', InputArgument::REQUIRED);
62 13
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function execute(InputInterface $input, OutputInterface $output)
68
    {
69
        $errOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
70
71
        $execution = $this->executionRepository->findByUuid($input->getArgument('uuid'));
72
        $handler = $this->handlerFactory->create($execution->getHandlerClass());
73
74
        try {
75
            $result = $handler->handle($execution->getWorkload());
76
        } catch (\Exception $e) {
77
            $errOutput->writeln($e->__toString());
78
79
            return 1;
80
        }
81
82
        $output->write(self::START_RESULT);
83
        $output->write($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $handler->handle($execution->getWorkload()) on line 75 can also be of type object<Serializable>; however, Symfony\Component\Consol...utputInterface::write() does only seem to accept string|array, 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...
84
        $output->write(self::END_RESULT);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function isHidden()
91
    {
92
        return true;
93
    }
94
}
95