Passed
Branch 1.0 (690a53)
by Vladimir
07:08
created

RouteServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 0
loc 62
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B register() 0 29 1
A buildPath() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Application;
6
7
use Whoops\Run;
8
use Zend\Diactoros\Response;
9
use League\Route\RouteCollection;
10
use Whoops\Handler\PrettyPageHandler;
11
use Zend\Diactoros\Response\SapiEmitter;
12
use Zend\Diactoros\ServerRequestFactory;
13
use League\Container\ServiceProvider\AbstractServiceProvider;
14
15
class RouteServiceProvider extends AbstractServiceProvider
16
{
17
    protected $provides = [
18
        'request',
19
        'response',
20
        'emitter',
21
        'router',
22
    ];
23
24
    private $prefix;
25
26
    public function __construct(string $prefix)
27
    {
28
        $this->prefix = $prefix;
29
    }
30
31
    /**
32
     * Use the register method to register items with the container via the
33
     * protected $this->container property or the `getContainer` method
34
     * from the ContainerAwareTrait.
35
     *
36
     * @return void
37
     */
38
    public function register(): void
39
    {
40
        $whoops = new Run;
41
        $whoops->pushHandler(new PrettyPageHandler);
0 ignored issues
show
Documentation introduced by
new \Whoops\Handler\PrettyPageHandler() is of type object<Whoops\Handler\PrettyPageHandler>, 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...
42
        $whoops->register();
43
44
        $this->getContainer()->share('request', function () {
45
            return ServerRequestFactory::fromGlobals(
46
                $_SERVER,
47
                $_GET,
48
                $_POST,
49
                $_COOKIE,
50
                $_FILES
51
            );
52
        });
53
        $this->getContainer()->share('response', Response::class);
54
        $this->getContainer()->share('emitter', SapiEmitter::class);
55
56
        $this->getContainer()->share('router', function () {
57
            $router = new RouteCollection($this->getContainer());
58
59
            $controller = new Controller($this->getContainer()->get(Kernel::class));
60
61
            $router->map('GET', $this->buildPath('/'), [$controller, 'index']);
62
            $router->map('GET', $this->buildPath('/channels/{name}'), [$controller, 'webhook']);
63
64
            return $router;
65
        });
66
    }
67
68
    private function buildPath(string $path): string
69
    {
70
        if ($this->prefix !== null) {
71
            return $this->prefix.'/'.$path;
72
        }
73
74
        return $path;
75
    }
76
}
77