Completed
Pull Request — master (#41)
by Wachter
20:46
created

ExecuteCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
rs 9.4285
cc 3
eloc 11
nc 4
nop 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\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
    /**
28
     * @var TaskHandlerFactoryInterface
29
     */
30
    private $handlerFactory;
31
32
    /**
33
     * @var TaskExecutionRepositoryInterface
34
     */
35
    private $executionRepository;
36
37
    /**
38
     * @param string $name
39
     * @param TaskHandlerFactoryInterface $handlerFactory
40
     * @param TaskExecutionRepositoryInterface $executionRepository
41
     */
42
    public function __construct(
43
        $name,
44
        TaskHandlerFactoryInterface $handlerFactory,
45
        TaskExecutionRepositoryInterface $executionRepository
46
    ) {
47
        parent::__construct($name);
48
49
        $this->handlerFactory = $handlerFactory;
50
        $this->executionRepository = $executionRepository;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function configure()
57
    {
58
        $this->addArgument('uuid', InputArgument::REQUIRED);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected function execute(InputInterface $input, OutputInterface $output)
65
    {
66
        $errOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
67
68
        $execution = $this->executionRepository->findByUuid($input->getArgument('uuid'));
69
        $handler = $this->handlerFactory->create($execution->getHandlerClass());
70
71
        try {
72
            $result = $handler->handle($execution->getWorkload());
73
        } catch (\Exception $e) {
74
            $errOutput->writeln($e->__toString());
75
76
            // Process exit-code: 0 = OK, >1 = FAIL
77
            return 1;
78
        }
79
80
        $output->write($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $handler->handle($execution->getWorkload()) on line 72 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...
81
82
        return 0;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function isHidden()
89
    {
90
        return true;
91
    }
92
}
93