Completed
Push — master ( cfc46b...5a6c91 )
by Thomas
09:47 queued 07:37
created

ExecuteCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 33.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 9
dl 0
loc 73
ccs 8
cts 24
cp 0.3333
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A configure() 0 4 1
B execute() 0 25 4
A isHidden() 0 4 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\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 13
    public function __construct(
44
        $name,
45
        TaskHandlerFactoryInterface $handlerFactory,
46
        TaskExecutionRepositoryInterface $executionRepository
47
    ) {
48 13
        parent::__construct($name);
49
50 13
        $this->handlerFactory = $handlerFactory;
51 13
        $this->executionRepository = $executionRepository;
52 13
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 13
    protected function configure()
58
    {
59 13
        $this->addArgument('uuid', InputArgument::REQUIRED);
60 13
    }
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'));
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|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...
87
88
        return 0;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function isHidden()
95
    {
96
        return true;
97
    }
98
}
99