Completed
Push — master ( 5e2812...881bca )
by Joschi
02:44
created

Server::dispatchRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * apparat-server
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Server
8
 * @subpackage  Apparat\Server\Ports
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Server\Ports;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Ports\Object;
41
use Apparat\Server\Domain\Contract\RouteInterface;
42
use Apparat\Server\Ports\Action\DayAction;
43
use Apparat\Server\Ports\Action\HourAction;
44
use Apparat\Server\Ports\Action\MinuteAction;
45
use Apparat\Server\Ports\Action\MonthAction;
46
use Apparat\Server\Ports\Action\ObjectAction;
47
use Apparat\Server\Ports\Action\SecondAction;
48
use Apparat\Server\Ports\Action\YearAction;
49
use Psr\Http\Message\ResponseInterface;
50
use Psr\Http\Message\ServerRequestInterface;
51
52
/**
53
 * Server facade
54
 *
55
 * @package Apparat\Server\Ports
56
 */
57
class Server
58
{
59
    /**
60
     * Server instance
61
     *
62
     * @var \Apparat\Server\Domain\Model\Server
63
     */
64
    protected static $server = null;
65
    /**
66
     * Default repository routes
67
     *
68
     * @array
69
     */
70
    const DEFAULT_ROUTES = [
71
        Route::SECOND => [
72
            '/{year}/{month}/{day}/{hour}/{minute}/{second}',
73
            self::TOKEN_YEAR +
74
            self::TOKEN_MONTH +
75
            self::TOKEN_DAY +
76
            self::TOKEN_HOUR +
77
            self::TOKEN_MINUTE +
78
            self::TOKEN_SECOND,
79
            SecondAction::class
80
        ],
81
        Route::MINUTE => [
82
            '/{year}/{month}/{day}/{hour}/{minute}',
83
            self::TOKEN_YEAR +
84
            self::TOKEN_MONTH +
85
            self::TOKEN_DAY +
86
            self::TOKEN_HOUR +
87
            self::TOKEN_MINUTE,
88
            MinuteAction::class
89
        ],
90
        Route::HOUR => [
91
            '/{year}/{month}/{day}/{hour}',
92
            self::TOKEN_YEAR +
93
            self::TOKEN_MONTH +
94
            self::TOKEN_DAY +
95
            self::TOKEN_HOUR,
96
            HourAction::class
97
        ],
98
        Route::DAY => [
99
            '/{year}/{month}/{day}',
100
            self::TOKEN_YEAR +
101
            self::TOKEN_MONTH +
102
            self::TOKEN_DAY,
103
            DayAction::class
104
        ],
105
        Route::MONTH => [
106
            '/{year}/{month}',
107
            self::TOKEN_YEAR +
108
            self::TOKEN_MONTH,
109
            MonthAction::class
110
        ],
111
        Route::YEAR => [
112
            '/{year}',
113
            self::TOKEN_YEAR,
114
            YearAction::class
115
        ]
116
    ];
117
    /**
118
     * Year route token
119
     *
120
     * @var array
121
     */
122
    const TOKEN_YEAR = ['year' => self::REGEX_ASTERISK.'|(?:\d{4})'];
123
    /**
124
     * Month route token
125
     *
126
     * @var array
127
     */
128
    const TOKEN_MONTH = ['month' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:1[0-2])'];
129
    /**
130
     * Day route token
131
     *
132
     * @var array
133
     */
134
    const TOKEN_DAY = ['day' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:[1-2]\d)|(?:3[0-1])'];
135
    /**
136
     * Hour route token
137
     *
138
     * @var array
139
     */
140
    const TOKEN_HOUR = ['hour' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:1[0-2])'];
141
    /**
142
     * Day route token
143
     *
144
     * @var array
145
     */
146
    const TOKEN_MINUTE = ['minute' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:[1-4]\d)|(?:5[0-9])'];
147
    /**
148
     * Second route token
149
     *
150
     * @var array
151
     */
152
    const TOKEN_SECOND = ['second' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:[1-4]\d)|(?:5[0-9])'];
153
    /**
154
     * Asterisk regular expression
155
     *
156
     * @var string
157
     */
158
    const REGEX_ASTERISK = '(?:\%2A)';
159
160
    /**
161
     * Register a route
162
     *
163
     * @param RouteInterface $route
164
     */
165 2
    public static function registerRoute(RouteInterface $route)
166
    {
167 2
        self::getServer()->registerRoute($route);
168 2
    }
169
170 1
    public static function registerRepositoryDefaultRoutes(/*RepositoryInterface $repository, $enable = true*/)
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between brackets of function declaration; 0 found
Loading history...
171
    {
172
        // Build the list of base routes
173 1
        $dateDefaultRoutes = array_slice(self::DEFAULT_ROUTES, 6 - getenv('OBJECT_DATE_PRECISION'));
174 1
        $numDefaultRoutes = count($dateDefaultRoutes);
175 1
        $baseDateRoute = current($dateDefaultRoutes);
176 1
        $enabledObjectTypes = '(?:-(?:(?:'.implode(')|(?:', array_map('preg_quote', Object::getSupportedTypes())).')))';
0 ignored issues
show
Bug introduced by
The method getSupportedTypes() does not seem to exist on object<Apparat\Object\Ports\Object>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
177
        $objectRoute = [
178 1
            $baseDateRoute[0].'/{hidden}{id}{type}{draft}{revision}{format}',
179 1
            $baseDateRoute[1] + [
180 1
                'hidden' => '\.?',
181 1
                'id' => self::REGEX_ASTERISK.'|(?:\d+)',
182 1
                'type' => $enabledObjectTypes.'?',
183 1
                'draft' => '(?:/(\.)?\\'.(2 + $numDefaultRoutes).')?',
184 1
                'revision' => '(?('.(5 + $numDefaultRoutes).')|(?:-\d+)?)',
185 1
                'format' => '(?('.(4 + $numDefaultRoutes).')(?:\.'.preg_quote(getenv('OBJECT_RESOURCE_EXTENSION')).')?)',
186 1
            ],
187
            ObjectAction::class
188 1
        ];
189 1
        $defaultRoutes = [Route::OBJECT => $objectRoute] + $dateDefaultRoutes;
190
191
        // Iterate through and register all base routes
192 1
        foreach ($defaultRoutes as $routeName => $routeConfig) {
193 1
            $route = new Route(Route::GET, $routeName, $routeConfig[0], $routeConfig[2], true);
194 1
            self::registerRoute($route->setTokens($routeConfig[1]));
195 1
        }
196 1
    }
197
198
    /**
199
     * Dispatch a request
200
     *
201
     * @param ServerRequestInterface $request
202
     * @return ResponseInterface $response
203
     */
204 2
    public static function dispatchRequest(ServerRequestInterface $request)
205
    {
206 2
        self::getServer()->dispatchRequest($request);
207 2
    }
208
209
    /**
210
     * Create and return the server instance
211
     *
212
     * @return \Apparat\Server\Domain\Model\Server Server instance
213
     */
214 2
    protected static function getServer()
215
    {
216 2
        if (self::$server === null) {
217 1
            self::$server = Kernel::create(\Apparat\Server\Domain\Model\Server::class);
218 1
        }
219 2
        return self::$server;
220
    }
221
}
222