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

App   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 69
ccs 0
cts 31
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 18 2
A registerContainer() 0 7 1
A registerRoute() 0 12 1
A getContainer() 0 4 1
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
}