Application::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Simply\Application;
4
5
use Simply\Application\Handler\MiddlewareHandler;
6
use Simply\Application\HttpFactory\HttpFactoryInterface;
7
8
/**
9
 * The core application that runs the main middleware stack.
10
 * @author Riikka Kalliomäki <[email protected]>
11
 * @copyright Copyright (c) 2018 Riikka Kalliomäki
12
 * @license http://opensource.org/licenses/mit-license.php MIT License
13
 */
14
class Application
15
{
16
    /** @var HttpFactoryInterface The factory used to generate the request from globals */
17
    private $httpFactory;
18
19
    /** @var MiddlewareHandler The middleware stack that is run for every request */
20
    private $middlewareStack;
21
22
    /** @var HttpClient The http client the application uses to communicate to the browser */
23
    private $httpClient;
24
25
    /**
26
     * Application constructor.
27
     * @param HttpFactoryInterface $factory The factory used to generate the request from globals
28
     * @param MiddlewareHandler $stack The middleware stack that is run for every request
29
     * @param HttpClient $client The http client the application uses to communicate to the browser
30
     */
31 12
    public function __construct(HttpFactoryInterface $factory, MiddlewareHandler $stack, HttpClient $client)
32
    {
33 12
        $this->httpFactory = $factory;
34 12
        $this->middlewareStack = $stack;
35 12
        $this->httpClient = $client;
36 12
    }
37
38
    /**
39
     * Runs the application middleware stack for the request and sends the response.
40
     */
41 12
    public function run(): void
42
    {
43 12
        $request = $this->httpFactory->createServerRequestFromGlobals();
44 12
        $response = $this->middlewareStack->handle($request);
45
46 12
        if ($request->getMethod() === 'HEAD') {
47 1
            $this->httpClient->omitBody();
48
        }
49
50 12
        $this->httpClient->send($response);
51 12
    }
52
}
53