Completed
Pull Request — master (#47)
by Wachter
12:32 queued 10:50
created

ExecuteCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 73
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isHidden() 0 4 1
A __construct() 0 10 1
A configure() 0 4 1
A execute() 0 25 4
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\Executor\FailedException;
20
use Task\Handler\TaskHandlerFactoryInterface;
21
use Task\Storage\TaskExecutionRepositoryInterface;
22
23
/**
24
 * Executes given execution identified by uuid.
25
 */
26
class ExecuteCommand extends Command
27
{
28
    /**
29
     * @var TaskHandlerFactoryInterface
30
     */
31
    private $handlerFactory;
32
33
    /**
34
     * @var TaskExecutionRepositoryInterface
35
     */
36
    private $executionRepository;
37
38
    /**
39
     * @param string $name
40
     * @param TaskHandlerFactoryInterface $handlerFactory
41
     * @param TaskExecutionRepositoryInterface $executionRepository
42
     */
43
    public function __construct(
44
        $name,
45
        TaskHandlerFactoryInterface $handlerFactory,
46
        TaskExecutionRepositoryInterface $executionRepository
47
    ) {
48
        parent::__construct($name);
49
50
        $this->handlerFactory = $handlerFactory;
51
        $this->executionRepository = $executionRepository;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function configure()
58
    {
59
        $this->addArgument('uuid', InputArgument::REQUIRED);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function execute(InputInterface $input, OutputInterface $output)
66
    {
67
        $errorOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
68
69
        $execution = $this->executionRepository->findByUuid($input->getArgument('uuid'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('uuid') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Task\Storage\TaskExecuti...Interface::findByUuid() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
70
        $handler = $this->handlerFactory->create($execution->getHandlerClass());
71
72
        try {
73
            $result = $handler->handle($execution->getWorkload());
74
        } catch (\Exception $exception) {
75
            if ($exception instanceof FailedException) {
76
                $errorOutput->writeln(FailedException::class);
77
                $exception = $exception->getPrevious();
78
            }
79
80
            $errorOutput->writeln($exception->__toString());
81
82
            // Process exit-code: 0 = OK, >1 = FAIL
83
            return 1;
84
        }
85
86
        $output->write($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $handler->handle($execution->getWorkload()) on line 73 can also be of type object<Serializable>; however, Symfony\Component\Consol...utputInterface::write() does only seem to accept string|object<Symfony\Co...onsole\Output\iterable>, 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...
87
88
        return 0;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function isHidden()
95
    {
96
        return true;
97
    }
98
}
99