Completed
Push — master ( 82d671...216ad2 )
by Gabor
03:54
created

WebApplication::setEnvironmentData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 6
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 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
namespace WebHemi\Application\Web;
13
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use WebHemi\Adapter\DependencyInjection\DependencyInjectionAdapterInterface;
17
use WebHemi\Adapter\Http\HttpAdapterInterface;
18
use WebHemi\Application\ApplicationInterface;
19
use WebHemi\Config\ConfigInterface;
20
use InvalidArgumentException;
21
22
/**
23
 * Class WebApplication.
24
 */
25
class WebApplication implements ApplicationInterface
26
{
27
    const MODULE_ADMIN = 'Admin';
28
    const MODULE_SITE = 'Website';
29
30
    /** @var DependencyInjectionAdapterInterface */
31
    private $container;
32
    /** @var ConfigInterface */
33
    private $config;
34
    /** @var array  */
35
    private $environmentData = [
36
        'GET'    => [],
37
        'POST'   => [],
38
        'SERVER' => [],
39
        'COOKIE' => [],
40
        'FILES'  => [],
41
    ];
42
43
    /**
44
     * ApplicationInterface constructor.
45
     *
46
     * @param DependencyInjectionAdapterInterface $container
47
     * @param ConfigInterface                     $config
48
     */
49
    public function __construct(DependencyInjectionAdapterInterface $container, ConfigInterface $config)
50
    {
51
        $this->container = $container;
52
        $this->config = $config;
53
    }
54
55
    /**
56
     * Returns the DI Adapter instance.
57
     *
58
     * @return DependencyInjectionAdapterInterface
59
     */
60
    public function getContainer()
61
    {
62
        return $this->container;
63
    }
64
65
    /**
66
     * Returns the Configuration.
67
     *
68
     * @return ConfigInterface
69
     */
70
    public function getConfig()
71
    {
72
        return $this->config;
73
    }
74
75
    /**
76
     * Sets application environments according to the super globals.
77
     *
78
     * @param string $key
79
     * @param array  $data
80
     *
81
     * @throws InvalidArgumentException
82
     *
83
     * @return $this
84
     */
85
    public function setEnvironmentData($key, array $data)
86
    {
87
        if (!isset($this->environmentData[$key])) {
88
            throw new InvalidArgumentException(sprintf('The key "%s" is not a valid superglobal name.', $key));
89
        }
90
91
        $this->environmentData[$key] = $data;
92
93
        return $this;
94
    }
95
96
    /**
97
     * Runs the application. This is where the magic happens.
98
     * According tho the environment settings this must build up the middleware pipeline and execute it.
99
     *
100
     * Pre-Routing Middleware can be; priority < 0:
101
     *  - LockCheck - check if the client IP is banned > S102|S403
102
     *  - Auth - if the user is not logged in, but there's a "Remember me" cookie, then logs in > S102
103
     *
104
     * Routing Middleware is fixed; priority = 0:
105
     *  - A middleware that routes the incoming Request and delegates to the matched middleware. > S102|S404|S405
106
     *    The RouteResult should be attached to the Request.
107
     *    If the Routing is not defined explicitly in the pipeline, then it will be injected with priority 0.
108
     *
109
     * Post-Routing Middleware can be; priority between 0 and 100:
110
     *  - Acl - checks if the given route is available for the client. Also checks the auth > S102|S401|S403
111
     *  - CacheReader - checks if a suitable response body is cached. > S102|S200
112
     *
113
     * Dispatcher Middleware is fixed; priority = 100:
114
     *  - A middleware which gets the corresponding Action middleware and applies it > S102|S500
115
     *    If the Dispatcher is not defined explicitly in the pipeline, then it will be injected with priority 100.
116
     *    The Dispatcher should not set the response Status Code to 200 to let Post-Dispatchers to be called.
117
     *
118
     * Post-Dispatch Middleware can be; priority > 100:
119
     *  - CacheWriter - writes response body into DataStorage (DB, File etc.) > S102
120
     *
121
     * Final Middleware is fixed:
122
     *  - This middleware behaves a bit differently. It cannot be ordered, it's always the last called middleware:
123
     *    - when the middleware pipeline reached its end (typically when the Status Code is still 102)
124
     *    - when one item of the middleware pipeline returns with return response (status code is set to 200|40*|500)
125
     *    - when during the pipeline process an Exception is thrown.
126
     *
127
     * When the middleware pipeline is finished the application prints the header and the output.
128
     *
129
     * @return void
130
     */
131
    public function run()
132
    {
133
        $this->container
134
            ->setServiceArgument(HttpAdapterInterface::class, $this->environmentData['SERVER'])
135
            ->setServiceArgument(HttpAdapterInterface::class, $this->environmentData['GET'])
136
            ->setServiceArgument(HttpAdapterInterface::class, $this->environmentData['POST'])
137
            ->setServiceArgument(HttpAdapterInterface::class, $this->environmentData['COOKIE'])
138
            ->setServiceArgument(HttpAdapterInterface::class, $this->environmentData['FILES']);
139
140
        /** @var HttpAdapterInterface $httpAdapter */
141
        $httpAdapter = $this->getContainer()->get(HttpAdapterInterface::class);
142
        /** @var ServerRequestInterface $request */
143
        $request = $httpAdapter->getRequest();
144
        /** @var ResponseInterface $response */
145
        $response = $httpAdapter->getResponse();
146
147
        /*  -- Pseudo code for the implementation
148
149
            var request
150
            var response
151
            var pipeline
152
            var finalMiddleware
153
154
            FOR var middleware IN pipeline
155
                response = CALL middleware WITH request, response
156
157
                IF response.header.statusCode IS NOT 102 THEN
158
                    BREAK
159
                END IF
160
            END FOR
161
162
            response = CALL finalMiddleware WITH request, response
163
164
            SEND response.header
165
            PRINT response.body
166
167
            EXIT
168
169
         */
170
171
        echo '<h1>Hello world!</h1>';
172
        echo $request->getMethod();
173
        echo $response->getBody();
174
    }
175
}
176