Passed
Push — master ( d7eb06...71752e )
by Gabor
02:39
created

AbstractAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 17
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Environment\ServiceInterface as EnvironmentInterface;
20
use WebHemi\Http\ResponseInterface;
21
use WebHemi\Http\ServerRequestInterface;
22
use WebHemi\Http\ServiceInterface as HttpInterface;
23
use WebHemi\I18n\ServiceInterface as I18nInterface;
24
25
/**
26
 * Class AbstractAdapter
27
 */
28
abstract class AbstractAdapter implements ServiceInterface
29
{
30
    /**
31
     * @var Throwable
32
     */
33
    public $error;
34
    /**
35
     * @var DependencyInjectionInterface
36
     */
37
    protected $container;
38
    /**
39
     * @var ServerRequestInterface
40
     */
41
    protected $request;
42
    /**
43
     * @var ResponseInterface
44
     */
45
    protected $response;
46
    /**
47
     * @var I18nInterface
48
     */
49
    protected $i18n;
50
51
    /**
52
     * ServiceAdapter constructor.
53
     *
54
     * @param DependencyInjectionInterface $container
55
     */
56 11
    public function __construct(DependencyInjectionInterface $container)
57
    {
58 11
        $this->container = $container;
59
        /**
60
         * @var HttpInterface $httpAdapter
61
         */
62 11
        $httpAdapter = $this->container->get(HttpInterface::class);
63
        /**
64
         * @var ServerRequestInterface $request
65
         */
66 11
        $this->request = $httpAdapter->getRequest();
67
        /**
68
         * @var ResponseInterface $response
69
         */
70 11
        $this->response = $httpAdapter->getResponse();
71
72 11
        $this->setApplicationUri();
73 11
    }
74
75
    /**
76
     * Sets the application URI into the request object.
77
     */
78 11
    protected function setApplicationUri()
79
    {
80
        /**
81
         * @var EnvironmentInterface $environmentManager
82
         */
83 11
        $environmentManager = $this->container->get(EnvironmentInterface::class);
84 11
        $applicationUri = rtrim($environmentManager->getSelectedApplicationUri(), '/');
85 11
        $this->request = $this->request
86 11
            ->withAttribute('applicationUri', $applicationUri);
87 11
    }
88
89
    /**
90
     * Starts the session.
91
     *
92
     * @return ServiceInterface
93
     */
94
    abstract public function initSession() : ServiceInterface;
95
96
    /**
97
     * Initializes the I18n Service.
98
     *
99
     * @return ServiceInterface
100
     */
101
    public function initInternationalization() : ServiceInterface
102
    {
103
        /** @var I18nInterface $instance */
104
        $instance = $this->container->get(I18nInterface::class);
105
        $this->i18n = $instance;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Runs the application. This is where the magic happens.
112
     * According tho the environment settings this must build up the middleware pipeline and execute it.
113
     *
114
     * a Pre-Routing Middleware can be; priority < 0:
115
     *  - LockCheck - check if the client IP is banned > S102|S403
116
     *  - Auth - if the user is not logged in, but there's a "Remember me" cookie, then logs in > S102
117
     *
118
     * Routing Middleware is fixed (RoutingMiddleware::class); priority = 0:
119
     *  - A middleware that routes the incoming Request and delegates to the matched middleware. > S102|S404|S405
120
     *    The RouteResult should be attached to the Request.
121
     *    If the Routing is not defined explicitly in the pipeline, then it will be injected with priority 0.
122
     *
123
     * a Post-Routing Middleware can be; priority between 0 and 100:
124
     *  - Acl - checks if the given route is available for the client. Also checks the auth > S102|S401|S403
125
     *  - CacheReader - checks if a suitable response body is cached. > S102|S200
126
     *
127
     * Dispatcher Middleware is fixed (DispatcherMiddleware::class); priority = 100:
128
     *  - A middleware which gets the corresponding Action middleware and applies it > S102
129
     *    If the Dispatcher is not defined explicitly in the pipeline, then it will be injected with priority 100.
130
     *    The Dispatcher should not set the response Status Code to 200 to let Post-Dispatchers to be called.
131
     *
132
     * a Post-Dispatch Middleware can be; priority > 100:
133
     *  - CacheWriter - writes response body into DataStorage (DB, File etc.) > S102
134
     *
135
     * Final Middleware is fixed (FinalMiddleware:class):
136
     *  - This middleware behaves a bit differently. It cannot be ordered, it's always the last called middleware:
137
     *    - when the middleware pipeline reached its end (typically when the Status Code is still 102)
138
     *    - when one item of the middleware pipeline returns with return response (status code is set to 200|40*|500)
139
     *    - when during the pipeline process an Exception is thrown.
140
     *
141
     * When the middleware pipeline is finished the application prints the header and the output.
142
     *
143
     * If a middleware other than the Routing, Dispatcher and Final Middleware has no priority set, it will be
144
     * considered to have priority = 50.
145
     *
146
     * @return ServiceInterface
147
     */
148
    abstract public function run() : ServiceInterface;
149
150
    /**
151
     * Renders the response body and sends it to the client.
152
     *
153
     * @return void
154
     *
155
     * @codeCoverageIgnore - no output for tests
156
     */
157
    abstract public function renderOutput() : void;
158
159
    /**
160
     * Sends the response body to the client.
161
     *
162
     * @return void
163
     *
164
     * @codeCoverageIgnore - no output for tests
165
     */
166
    abstract public function sendOutput() : void;
167
}
168