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

Socket   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 0
cbo 7
dl 0
loc 42
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C handle() 0 27 7
A loadCommands() 0 6 1
1
<?php
2
3
namespace Padawan\Framework\Application;
4
5
use Amp;
6
use Padawan\Command\AsyncCommand;
7
use Padawan\Command\KillCommand;
8
use Padawan\Command\ListCommand;
9
use Padawan\Framework\Application;
10
use Padawan\Command\CompleteCommand;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Padawan\Framework\Application\Socket\SocketOutput;
13
14
/**
15
 * Class Socket
16
 */
17
class Socket extends Application
18
{
19
    public function __construct()
20
    {
21
        parent::__construct("Padawan Server");
22
    }
23
24
    public function handle($request, SocketOutput $output)
25
    {
26
        if (!$request
27
            || !property_exists($request, "command")
28
            || !property_exists($request, "params")) {
29
            yield $output->write(json_encode([
30
                "error" => "Bad request"
31
            ]));
32
            return;
33
        }
34
        $arrayForInput = ['command' => $request->command];
35
        foreach($request->params as $key=>$value) {
36
            $arrayForInput[$key] =  $value;
37
        }
38
        $input = new ArrayInput($arrayForInput);
39
        $command = $this->find($request->command);
40
        try {
41
            if ($command instanceof AsyncCommand) {
42
                yield Amp\resolve($command->run($input, $output));
0 ignored issues
show
Bug introduced by
It seems like $command->run($input, $output) can be null; however, Amp\resolve() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
43
            }
44
        } catch (\Exception $e) {
45
            printf("Error: %s\n", $e->getMessage());
46
            yield $output->write(json_encode([
47
                "error" => $e->getMessage()
48
            ]));
49
        }
50
    }
51
52
    protected function loadCommands()
53
    {
54
        $this->add(new CompleteCommand);
55
        $this->add(new ListCommand);
56
        $this->add(new KillCommand);
57
    }
58
}
59