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

Socket::handle()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 6.7272
cc 7
eloc 19
nc 7
nop 2
1
<?php
2
3
namespace Padawan\Framework\Application;
4
5
use Amp;
6
use Padawan\Command\KillCommand;
7
use Padawan\Command\ListCommand;
8
use Padawan\Framework\Application;
9
use Padawan\Command\CompleteCommand;
10
use Symfony\Component\Console\Input\ArrayInput;
11
use Padawan\Framework\Application\Socket\SocketOutput;
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
            if ($command instanceof AsyncCommand) {
0 ignored issues
show
Bug introduced by
The class Padawan\Framework\Application\AsyncCommand does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
41
                yield Amp\resolve($command->run($input, $output));
42
            }
43
        } catch (\Exception $e) {
44
            printf("Error: %s\n", $e->getMessage());
45
            yield $output->write(json_encode([
46
                "error" => $e->getMessage()
47
            ]));
48
        }
49
    }
50
51
    protected function loadCommands()
52
    {
53
        $this->add(new CompleteCommand);
54
        $this->add(new ListCommand);
55
        $this->add(new KillCommand);
56
    }
57
}
58