Completed
Push — master ( c6bc00...58eb61 )
by Pieter
05:28 queued 02:20
created

server.php (8 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php declare(strict_types = 1);
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 65 and the first side effect is on line 17.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
use Aerys\Host;
4
use Amp\Redis\Client;
5
use Auryn\Injector;
6
use ekinhbayar\GitAmp\Github\Credentials;
7
use ekinhbayar\GitAmp\Storage\Counter;
8
use ekinhbayar\GitAmp\Storage\NativeCounter;
9
use ekinhbayar\GitAmp\Websocket\Handler;
10
use Monolog\Logger;
11
use Monolog\Handler\StreamHandler;
12
use Psr\Log\LoggerInterface;
13
use function Aerys\root;
14
use function Aerys\router;
15
use function Aerys\websocket;
16
17
$configuration = require_once __DIR__ . '/config.php';
18
19
$injector = new Injector;
20
21
$injector->alias(Counter::class, NativeCounter::class);
22
23
$injector->alias(Credentials::class, get_class($configuration['github']));
24
25
$injector->share($configuration['github']);
26
27
$injector->alias(LoggerInterface::class, Logger::class);
28
$injector->share(Logger::class);
29
30
$injector->delegate(Logger::class, function() use ($configuration) {
31
    $logger = new Logger('gitamp');
32
33
    $logger->pushHandler(new StreamHandler('php://stdout', $configuration['logLevel']));
34
35
    return $logger;
36
});
37
38
// @todo unuglify this
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
39
if (isset($configuration['ssl'])) {
40
    $origin = 'https://' . $configuration['hostname'];
41
42 View Code Duplication
    if ($configuration['ssl']['port'] !== 443) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
        $origin .= ':' . $configuration['ssl']['port'];
44
    }
45
} else {
46
    $origin = 'http://' . $configuration['hostname'];
47
48 View Code Duplication
    if ($configuration['expose']['port'] !== 80) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
        $origin .= ':' . $configuration['expose']['port'];
50
    }
51
}
52
53
$injector->define(Handler::class, [
54
    ':origin' => $origin,
55
]);
56
57
$injector->define(Client::class, [
58
    ':uri' => $configuration['redis']
59
]);
60
61
$websocket = $injector->make(Handler::class);
62
63
$router = router()->get('/ws', websocket($websocket));
64
65
$requestLogger = new class implements \Aerys\Bootable {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
66
    private $logger;
67
68
    public function boot(\Aerys\Server $server, \Psr\Log\LoggerInterface $logger)
69
    {
70
        $this->logger = $logger;
71
    }
72
73
    public function __invoke(\Aerys\Request $req, \Aerys\Response $res)
74
    {
75
        $this->logger->debug('Incoming request', [
76
            'method'     => $req->getMethod(),
77
            'uri'        => $req->getUri(),
78
            'headers'    => $req->getAllHeaders(),
79
            'parameters' => $req->getAllParams(),
80
            'body'       => $req->getBody(),
81
        ]);
82
    }
83
};
84
85
if (isset($configuration['ssl'])) {
86
    (new Host())
87
        ->name($configuration['hostname'])
88
        ->expose($configuration['expose']['ip'], $configuration['expose']['port'])
89
        ->use($requestLogger)
90
        ->redirect('https://' . $configuration['hostname'])
91
    ;
92
93
    (new Host())
94
        ->name($configuration['hostname'])
95
        ->expose($configuration['ssl']['ip'], $configuration['ssl']['port'])
96
        ->encrypt($configuration['ssl']['certificate'], $configuration['ssl']['key'])
97
        ->use($requestLogger)
98
        ->use($router)
99
        ->use(root(__DIR__ . '/public'))
0 ignored issues
show
\Aerys\root(__DIR__ . '/public') is of type object<Aerys\Bootable>, but the function expects a callable.

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...
100
    ;
101
} else {
102
    (new Host())
103
        ->name($configuration['hostname'])
104
        ->expose($configuration['expose']['ip'], $configuration['expose']['port'])
105
        ->use($requestLogger)
106
        ->use($router)
107
        ->use(root(__DIR__ . '/public'))
0 ignored issues
show
\Aerys\root(__DIR__ . '/public') is of type object<Aerys\Bootable>, but the function expects a callable.

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...
108
    ;
109
}
110
111
$logger = $injector->make(LoggerInterface::class);
112
113
\Amp\onError(function(Throwable $e) use ($logger) {
114
    $logger->emergency('GitAmp blew up', ['exception' => $e]);
115
});
116