App   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 4
dl 0
loc 77
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 17 2
A getContainer() 0 4 1
A registerRoute() 0 12 1
A registerServices() 0 5 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\Http\Exception\NotFoundException;
13
use League\Route\RouteCollection;
14
15
class App
16
{
17
    /**
18
     * @var Container
19
     */
20
    private $container;
21
22
    /**
23
     * @var RouteCollection
24
     */
25
    private $route;
26
27
    /**
28
    * @var CoreServiceProvider
29
    */
30
    private $serviceProvider;
31
32
    /**
33
     * App constructor.
34
     *
35
     * @param Container $container
36
     */
37
    public function __construct(Container $container)
38
    {
39
        $this->container = $container;
40
        $this->registerServices();
41
        $this->registerRoute();
42
    }
43
44
    /**
45
     * @return mixed|\Psr\Http\Message\ResponseInterface
46
     */
47
    final public function handle()
48
    {
49
      $request = $this->container->get('request');
50
      $response = $this->container->get('response');
51
52
      try {
53
          return $this->route->dispatch(
54
              $request,
55
              $response
56
            );
57
        } catch (NotFoundException $exception) {
58
            /**
59
             * handle 404
60
             */
61
            return $response->errorNotFound();
62
        }
63
    }
64
65
    /**
66
     * @return Container
67
     */
68
    final public function getContainer(): Container
69
    {
70
        return $this->container;
71
    }
72
73
    private function registerRoute()
74
    {
75
        /**
76
         * instantiate $route object
77
         */
78
        $this->route = new RouteCollection($this->container);
79
80
        /**
81
         * Register all routes mapping to $route object
82
         */
83
        \PhpBootstrap\Routes::collections($this->route, $this->container);
84
    }
85
86
    private function registerServices()
87
    {
88
        $this->serviceProvider = new CoreServiceProvider();
89
        $this->container->addServiceProvider($this->serviceProvider);
90
    }
91
}