Completed
Pull Request — master (#29)
by Aleh
02:51
created

Socket::handle()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 8.439
cc 6
eloc 18
nc 5
nop 2
1
<?php
2
3
namespace Padawan\Framework\Application;
4
5
use Padawan\Framework\Application;
6
use Padawan\Framework\Application\Socket\SocketOutput;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Padawan\Command\CompleteCommand;
9
use Padawan\Command\KillCommand;
10
use Padawan\Command\ListCommand;
11
use Amp;
12
13
/**
14
 * Class Socket
15
 */
16
class Socket extends Application
17
{
18
    public function __construct()
19
    {
20
        parent::__construct("Padawan Server");
21
    }
22
23
    public function handle($request, SocketOutput $output)
24
    {
25
        if (!$request
26
            || !property_exists($request, "command")
27
            || !property_exists($request, "params")) {
28
            yield $output->write(json_encode([
29
                "error" => "Bad request"
30
            ]));
31
            return;
32
        }
33
        $arrayForInput = ['command' => $request->command];
34
        foreach($request->params as $key=>$value) {
35
            $arrayForInput[$key] =  $value;
36
        }
37
        $input = new ArrayInput($arrayForInput);
38
        $command = $this->find($request->command);
39
        try {
40
            yield Amp\resolve($command->run($input, $output));
0 ignored issues
show
Documentation introduced by
$command->run($input, $output) is of type integer, but the function expects a object<Generator>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
        } catch (\Exception $e) {
42
            printf("Error: %s\n", $e->getMessage());
43
            yield $output->write(json_encode([
44
                "error" => $e->getMessage()
45
            ]));
46
        }
47
    }
48
49
    protected function loadCommands()
50
    {
51
        $this->add(new CompleteCommand);
52
        $this->add(new ListCommand);
53
        $this->add(new KillCommand);
54
    }
55
}
56