Completed
Push — master ( da1cfa...dccc7f )
by Vladimir
03:06
created

RouteServiceProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Application;
6
7
use Zend\Diactoros\Response;
8
use League\Route\RouteCollection;
9
use Zend\Diactoros\Response\SapiEmitter;
10
use Zend\Diactoros\ServerRequestFactory;
11
use League\Container\ServiceProvider\AbstractServiceProvider;
12
13
class RouteServiceProvider extends AbstractServiceProvider
14
{
15
    protected $provides = [
16
        'request',
17
        'response',
18
        'emitter',
19
        'router',
20
    ];
21
22
    private $prefix;
23
24
    public function __construct(string $prefix)
25
    {
26
        $this->prefix = $prefix;
27
    }
28
29
    /**
30
     * Use the register method to register items with the container via the
31
     * protected $this->container property or the `getContainer` method
32
     * from the ContainerAwareTrait.
33
     *
34
     * @return void
35
     */
36
    public function register(): void
37
    {
38
        $this->getContainer()->share('request', function () {
39
            return ServerRequestFactory::fromGlobals(
40
                $_SERVER,
41
                $_GET,
42
                $_POST,
43
                $_COOKIE,
44
                $_FILES
45
            );
46
        });
47
        $this->getContainer()->share('response', Response::class);
48
        $this->getContainer()->share('emitter', SapiEmitter::class);
49
50
        $this->getContainer()->share('router', function () {
51
            $router = new RouteCollection($this->getContainer());
52
53
            $controller = new Controller($this->getContainer()->get(Kernel::class));
54
55
            $router->map('GET', $this->buildPath('/'), [$controller, 'index']);
56
            $router->map('GET', $this->buildPath('/channels/{name}'), [$controller, 'webhook']);
57
58
            return $router;
59
        });
60
    }
61
62
    private function buildPath(string $path): string
63
    {
64
        if ($this->prefix !== null) {
65
            return $this->prefix.'/'.$path;
66
        }
67
68
        return $path;
69
    }
70
}
71