App::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Albert221\Blog;
4
5
use Albert221\Blog\Route\RouteCollection;
6
use League\Container\Container;
7
use League\Container\ReflectionContainer;
8
use League\Route\Http\Exception\NotFoundException;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Zend\Diactoros\Request;
12
use Zend\Diactoros\Response;
13
use Zend\Diactoros\Response\EmitterInterface;
14
15
class App
16
{
17
    /**
18
     * @var Container
19
     */
20
    protected $container;
21
22
    public function __construct()
23
    {
24
        $this->container = new Container;
25
        $this->container->delegate(new ReflectionContainer);
26
27
        $this->container->add('baseDir', dirname(__DIR__));
28
29
        $this->loadConfig();
30
        $this->loadServiceProviders();
31
    }
32
33
    private function loadConfig()
34
    {
35
        $this->container->add(
36
            'config',
37
            require $this->container->get('baseDir').'/config/config.php'
38
        );
39
    }
40
41
    private function loadServiceProviders()
42
    {
43
        foreach ($this->container->get('config')['serviceProviders'] as $provider) {
44
            $this->container->addServiceProvider($provider);
45
        }
46
    }
47
48
    /**
49
     * Sends response.
50
     */
51
    public function run()
52
    {
53
        /** @var RouteCollection $route */
54
        $route = $this->container->get(RouteCollection::class);
55
        /** @var ServerRequestInterface $request */
56
        $request = $this->container->get(ServerRequestInterface::class);
57
        /** @var ResponseInterface $response */
58
        $response = $this->container->get(ResponseInterface::class);
59
        /** @var EmitterInterface $emitter */
60
        $emitter = $this->container->get(EmitterInterface::class);
61
62
        $response = $route->dispatch($request, $response);
63
64
        $emitter->emit($response);
65
    }
66
}
67