Completed
Push — master ( 5bda77...cb2c73 )
by Aleh
10s
created

AsyncCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 9
c 2
b 0
f 1
lcom 1
cbo 3
dl 0
loc 66
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 31 5
A execute() 0 6 2
executeAsync() 0 1 ?
A getContainer() 0 4 1
A getApplication() 0 4 1
1
<?php
2
3
namespace Padawan\Command;
4
5
use DI\Container;
6
use Padawan\Framework\Application\Socket;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Padawan\Framework\Application\Socket\SocketOutput;
11
use Symfony\Component\Console\Exception\ExceptionInterface;
12
13
abstract class AsyncCommand extends Command
14
{
15
16
    /**
17
     * @return \Generator
18
     */
19
    public function run(InputInterface $input, OutputInterface $output)
20
    {
21
        // force the creation of the synopsis before the merge with the app definition
22
        $this->getSynopsis(true);
23
        $this->getSynopsis(false);
24
25
        // add the application arguments and options
26
        $this->mergeApplicationDefinition();
27
28
        // bind the input against the command specific arguments/options
29
        try {
30
            $input->bind($this->getDefinition());
31
        } catch (ExceptionInterface $e) {
32
            if (!$this->ignoreValidationErrors()) {
33
                throw $e;
34
            }
35
        }
36
37
        $this->initialize($input, $output);
38
39
        // The command name argument is often omitted when a command is executed directly with its run() method.
40
        // It would fail the validation if we didn't make sure the command argument is present,
41
        // since it's required by the application.
42
        if ($input->hasArgument('command') && null === $input->getArgument('command')) {
43
            $input->setArgument('command', $this->getName());
44
        }
45
46
        $input->validate();
47
48
        return $this->execute($input, $output);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->execute($input, $output); of type Generator|null adds the type Generator to the return on line 48 which is incompatible with the return type of the parent method Symfony\Component\Console\Command\Command::run of type integer.
Loading history...
49
    }
50
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        if ($output instanceof SocketOutput) {
54
            return $this->executeAsync($input, $output);
55
        }
56
    }
57
58
    /**
59
     * @return \Generator
60
     */
61
    abstract protected function executeAsync(InputInterface $input, SocketOutput $output);
62
63
    /**
64
     * @return Container
65
     */
66
    public function getContainer()
67
    {
68
        return $this->getApplication()->getContainer();
69
    }
70
71
    /**
72
     * @return Socket
73
     */
74
    public function getApplication()
75
    {
76
        return parent::getApplication();
77
    }
78
}
79