Passed
Push — master ( 10d55f...e14a05 )
by Gabor
03:02
created

AbstractAdapter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
dl 0
loc 123
ccs 6
cts 10
cp 0.6
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

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