Passed
Push — master ( 6282ae...86ce5d )
by Gabor
04:01
created

AbstractAdapter::invokeMiddleware()   B

Complexity

Conditions 5
Paths 14

Size

Total Lines 48
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 27
cts 27
cp 1
rs 8.551
c 0
b 0
f 0
cc 5
eloc 31
nc 14
nop 3
crap 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
AbstractAdapter::run() 0 1 ?
AbstractAdapter::renderOutput() 0 1 ?
AbstractAdapter::sendOutput() 0 1 ?
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2018 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Application\ServiceAdapter;
15
16
use Throwable;
17
use WebHemi\Application\ServiceInterface;
18
use WebHemi\DependencyInjection\ServiceInterface as DependencyInjectionInterface;
19
use WebHemi\Http\ResponseInterface;
20
use WebHemi\Http\ServerRequestInterface;
21
use WebHemi\Http\ServiceInterface as HttpInterface;
22
use WebHemi\I18n\ServiceInterface as I18nInterface;
23
24
/**
25
 * Class AbstractAdapter
26
 */
27
abstract class AbstractAdapter implements ServiceInterface
28
{
29
    /** @var Throwable */
30
    public $error;
31
    /** @var DependencyInjectionInterface */
32
    protected $container;
33
    /** @var ServerRequestInterface */
34
    protected $request;
35
    /** @var ResponseInterface */
36
    protected $response;
37
    /** @var I18nInterface */
38
    protected $i18n;
39
40
    /**
41
     * ServiceAdapter constructor.
42
     *
43
     * @param DependencyInjectionInterface $container
44
     */
45 11
    public function __construct(DependencyInjectionInterface $container)
46
    {
47 11
        $this->container = $container;
48
        /** @var HttpInterface $httpAdapter */
49 11
        $httpAdapter = $this->container->get(HttpInterface::class);
50
        /** @var ServerRequestInterface $request */
51 11
        $this->request = $httpAdapter->getRequest();
52
        /** @var ResponseInterface $response */
53 11
        $this->response = $httpAdapter->getResponse();
54 11
    }
55
56
    /**
57
     * Starts the session.
58
     *
59
     * @return ServiceInterface
60
     */
61
    abstract public function initSession() : ServiceInterface;
62
63
    /**
64
     * Initializes the I18n Service.
65
     *
66
     * @return ServiceInterface
67
     */
68
    public function initInternationalization() : ServiceInterface
69
    {
70
        $this->i18n = $this->container->get(I18nInterface::class);
71
72
        return $this;
73
    }
74
75
    /**
76
     * Runs the application. This is where the magic happens.
77
     * According tho the environment settings this must build up the middleware pipeline and execute it.
78
     *
79
     * a Pre-Routing Middleware can be; priority < 0:
80
     *  - LockCheck - check if the client IP is banned > S102|S403
81
     *  - Auth - if the user is not logged in, but there's a "Remember me" cookie, then logs in > S102
82
     *
83
     * Routing Middleware is fixed (RoutingMiddleware::class); priority = 0:
84
     *  - A middleware that routes the incoming Request and delegates to the matched middleware. > S102|S404|S405
85
     *    The RouteResult should be attached to the Request.
86
     *    If the Routing is not defined explicitly in the pipeline, then it will be injected with priority 0.
87
     *
88
     * a Post-Routing Middleware can be; priority between 0 and 100:
89
     *  - Acl - checks if the given route is available for the client. Also checks the auth > S102|S401|S403
90
     *  - CacheReader - checks if a suitable response body is cached. > S102|S200
91
     *
92
     * Dispatcher Middleware is fixed (DispatcherMiddleware::class); priority = 100:
93
     *  - A middleware which gets the corresponding Action middleware and applies it > S102
94
     *    If the Dispatcher is not defined explicitly in the pipeline, then it will be injected with priority 100.
95
     *    The Dispatcher should not set the response Status Code to 200 to let Post-Dispatchers to be called.
96
     *
97
     * a Post-Dispatch Middleware can be; priority > 100:
98
     *  - CacheWriter - writes response body into DataStorage (DB, File etc.) > S102
99
     *
100
     * Final Middleware is fixed (FinalMiddleware:class):
101
     *  - This middleware behaves a bit differently. It cannot be ordered, it's always the last called middleware:
102
     *    - when the middleware pipeline reached its end (typically when the Status Code is still 102)
103
     *    - when one item of the middleware pipeline returns with return response (status code is set to 200|40*|500)
104
     *    - when during the pipeline process an Exception is thrown.
105
     *
106
     * When the middleware pipeline is finished the application prints the header and the output.
107
     *
108
     * If a middleware other than the Routing, Dispatcher and Final Middleware has no priority set, it will be
109
     * considered to have priority = 50.
110
     *
111
     * @return ServiceInterface
112
     */
113
    abstract public function run() : ServiceInterface;
114
115
    /**
116
     * Renders the response body and sends it to the client.
117
     *
118
     * @return void
119
     *
120
     * @codeCoverageIgnore - no output for tests
121
     */
122
    abstract public function renderOutput() : void;
123
124
    /**
125
     * Sends the response body to the client.
126
     *
127
     * @return void
128
     *
129
     * @codeCoverageIgnore - no output for tests
130
     */
131
    abstract public function sendOutput() : void;
132
}
133