Passed
Push — master ( f656f1...1f40e6 )
by Harry Osmar
27s
created

App::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: harry
5
 * Date: 3/15/18
6
 * Time: 10:03 AM
7
 */
8
9
namespace PhpBootstrap;
10
11
use League\Container\Container;
12
use League\Route\RouteCollection;
13
14
class App
15
{
16
    /**
17
     * @var Container
18
     */
19
    private $container;
20
    /**
21
     * @var RouteCollection
22
     */
23
    private $route;
24
25
    public function __construct(Container $container)
26
    {
27
        $this->container = $container;
28
        $this->registerContainer();
29
        $this->registerRoute();
30
    }
31
32
    /**
33
     * @return mixed|\Psr\Http\Message\ResponseInterface
34
     */
35
    final public function handle()
36
    {
37
        try {
38
            /**
39
             * process the request
40
             */
41
            return $this->route->dispatch(
42
                $this->container->get('request'),
43
                $this->container->get(\PhpBootstrap\Contracts\Response::class)
44
            );
45
        } catch (\League\Route\Http\Exception\NotFoundException $exception) {
46
            /**
47
             * handle 404
48
             */
49
            $response = new \PhpBootstrap\Services\Response();
50
            return $response->errorNotFound();
51
        }
52
    }
53
54
    private function registerContainer()
55
    {
56
        /**
57
         * Register all service providers to $container
58
         */
59
        \PhpBootstrap\ServiceProviders::register($this->container);
60
    }
61
62
    private function registerRoute()
63
    {
64
        /**
65
         * instantiate $route object
66
         */
67
        $this->route = new RouteCollection($this->container);
68
69
        /**
70
         * Register all routes mapping to $route object
71
         */
72
        \PhpBootstrap\Routes::collections($this->route, $this->container);
73
    }
74
75
    /**
76
     * @return Container
77
     */
78
    public function getContainer(): Container
79
    {
80
        return $this->container;
81
    }
82
}