Mika::createDi()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace OpenEngine\Mika\Core;
4
5
use OpenEngine\Di\Di;
6
use OpenEngine\Di\DiConfig;
7
use OpenEngine\Di\Exceptions\ClassNotFoundException;
8
use OpenEngine\Di\Exceptions\MethodNotFoundException;
9
use OpenEngine\Di\Exceptions\MissingMethodArgumentException;
10
use OpenEngine\Http\Exceptions\NotFoundHttpException;
11
use OpenEngine\Http\Message\Request\RequestFactory;
12
use OpenEngine\Http\Message\Uri\UriFactory;
13
use OpenEngine\Mika\Core\Components\Route\Interfaces\RouteInterface;
14
use Psr\Http\Message\RequestInterface;
15
16
class Mika
17
{
18
    /**
19
     * @param DiConfig $diConfig
20
     * @throws ClassNotFoundException
21
     * @throws MethodNotFoundException
22
     * @throws MissingMethodArgumentException
23
     * @throws NotFoundHttpException
24
     */
25
    public static function run(DiConfig $diConfig): void
26
    {
27
        /**
28
         * @var RouteInterface|Route $route
29
         */
30
        $di = self::createDi($diConfig);
31
        $route = $di->get(RouteInterface::class);
32
33
        $response = $route->callControllerAction();
34
35
        foreach ($response->getHeaders() as $name => $header) {
36
            header($name . ': ' . $response->getHeaderLine($name));
37
        }
38
39
        print $response->getBody();
40
    }
41
42
    /**
43
     * @param DiConfig $diConfig
44
     * @return Di
45
     */
46
    private static function createDi(DiConfig $diConfig): Di
47
    {
48
        $diConfig->registerObject(RequestInterface::class, self::createRequest());
49
        return new Di($diConfig);
50
    }
51
52
    /**
53
     * @return RequestInterface
54
     */
55
    private static function createRequest(): RequestInterface
56
    {
57
        $uri = (new UriFactory())->createUri();
58
        return (new RequestFactory())->createRequest($_SERVER['REQUEST_METHOD'] ?? 'GET', $uri);
59
    }
60
}
61