GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( deffcb...b633e9 )
by Anton
02:17
created

Server::start()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 16
ccs 9
cts 12
cp 0.75
crap 2.0625
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
4
namespace Deployer\Executor;
5
6
use Deployer\Deployer;
7
use Deployer\Exception\Exception;
8
use Deployer\Task\Context;
9
use Psr\Http\Message\ServerRequestInterface;
10
use React;
11
use React\Http\Message\Response;
12
use Symfony\Component\Console\Helper\QuestionHelper;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Throwable;
16
use function Deployer\getHost;
17
18
class Server
19
{
20
    private $input;
21
    private $output;
22
    private $questionHelper;
23
    private $loop;
24
    private $port;
25
26 11
    public function __construct(
27
        InputInterface $input,
28
        OutputInterface $output,
29
        QuestionHelper $questionHelper
30
    )
31
    {
32 11
        $this->input = $input;
33 11
        $this->output = $output;
34 11
        $this->questionHelper = $questionHelper;
35 11
    }
36
37 11
    public function start()
38
    {
39 11
        $this->loop = React\EventLoop\Factory::create();
40
        $server = new React\Http\Server($this->loop, function (ServerRequestInterface $request) {
41
            try {
42 1
                return $this->router($request);
43
            } catch (Throwable $exception) {
44
                Deployer::printException($this->output, $exception);
45
                return new React\Http\Message\Response(500, ['Content-Type' => 'text/plain'], 'Master error: ' . $exception->getMessage());
46
            }
47 11
        });
48 11
        $socket = new React\Socket\Server(0, $this->loop);
49 11
        $server->listen($socket);
50 11
        $address = $socket->getAddress();
51 11
        $this->port = parse_url($address, PHP_URL_PORT);
52 11
    }
53
54 1
    private function router(ServerRequestInterface $request): Response
55
    {
56 1
        switch ($request->getUri()->getPath()) {
57 1
            case '/proxy':
58 1
                $body = $request->getBody();
59 1
                ['host' => $host, 'func' => $func, 'arguments' => $arguments] = json_decode($body, true);
0 ignored issues
show
Bug introduced by Anton Medvedev
The variable $host does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by Anton Medvedev
The variable $func does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by Anton Medvedev
The variable $arguments does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
60
61 1
                Context::push(new Context(getHost($host), $this->input, $this->output));
62 1
                $answer = call_user_func($func, ...$arguments);
63 1
                Context::pop();
64
65 1
                return new Response(200, ['Content-Type' => 'application/json'], json_encode($answer));
66
                break;
0 ignored issues
show
Unused Code introduced by Anton Medvedev
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
67
68
            default:
69
                throw new Exception('Server path not found: ' . $request->getUri()->getPath());
70
        }
71
    }
72
73 2
    public function addPeriodicTimer($interval, $callback)
74
    {
75 2
        $this->loop->addPeriodicTimer($interval, $callback);
76 2
    }
77
78 2
    public function run()
79
    {
80 2
        $this->loop->run();
81 2
    }
82
83 2
    public function stop()
84
    {
85 2
        $this->loop->stop();
86 2
    }
87
88 2
    public function getPort(): int
89
    {
90 2
        return $this->port;
91
    }
92
}
93