Completed
Push — master ( 1377c9...6f32f0 )
by Joschi
02:51
created

Server   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 9
c 4
b 0
f 2
lcom 1
cbo 3
dl 0
loc 199
ccs 62
cts 62
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A registerRoute() 0 4 1
A registerRepositoryDefaultRoutes() 0 17 3
A dispatchRequest() 0 4 1
A getServer() 0 7 2
A buildDefaultDateRoutes() 0 53 1
A buildDefaultObjectRoutes() 0 20 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
     * Year route token
67
     *
68
     * @var array
69
     */
70
    protected static $TOKEN_YEAR = ['year' => self::REGEX_ASTERISK.'|(?:\d{4})'];
71
    /**
72
     * Month route token
73
     *
74
     * @var array
75
     */
76
    protected static $TOKEN_MONTH = ['month' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:1[0-2])'];
77
    /**
78
     * Day route token
79
     *
80
     * @var array
81
     */
82
    protected static $TOKEN_DAY = ['day' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:[1-2]\d)|(?:3[0-1])'];
83
    /**
84
     * Hour route token
85
     *
86
     * @var array
87
     */
88
    protected static $TOKEN_HOUR = ['hour' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:1[0-2])'];
89
    /**
90
     * Day route token
91
     *
92
     * @var array
93
     */
94
    protected static $TOKEN_MINUTE = ['minute' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:[1-4]\d)|(?:5[0-9])'];
95
    /**
96
     * Second route token
97
     *
98
     * @var array
99
     */
100
    protected static $TOKEN_SECOND = ['second' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:[1-4]\d)|(?:5[0-9])'];
101
    /**
102
     * Asterisk regular expression
103
     *
104
     * @var string
105
     */
106
    const REGEX_ASTERISK = '(?:\%2A)';
107
108
    /**
109
     * Register a route
110
     *
111
     * @param RouteInterface $route
112
     */
113 2
    public static function registerRoute(RouteInterface $route)
114
    {
115 2
        self::getServer()->registerRoute($route);
116 2
    }
117
118
    /**
119
     * Register the default routes for a particular repository
120
     *
121
     * @param string $repositoryPath Repository path
122
     * @param bool $enable Enable / disable default routes
123
     */
124 1
    public static function registerRepositoryDefaultRoutes($repositoryPath = '', $enable = true)
0 ignored issues
show
Unused Code introduced by
The parameter $enable is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
    {
126
        // Repository route prefix
127 1
        $prefix = rtrim('/'.$repositoryPath, '/');
128
129
        // Build the list of base routes
130 1
        $dateDefaultRoutes = self::buildDefaultDateRoutes($prefix, getenv('OBJECT_DATE_PRECISION'));
131 1
        $baseDateRoute = count($dateDefaultRoutes) ? current($dateDefaultRoutes) : ['/', []];
132 1
        $defaultRoutes = self::buildDefaultObjectRoutes($prefix, $baseDateRoute, count($dateDefaultRoutes))
133 1
            + $dateDefaultRoutes;
134
135
        // Iterate through and register all base routes
136 1
        foreach ($defaultRoutes as $routeName => $routeConfig) {
137 1
            $route = new Route(Route::GET, $routeName, $routeConfig[0], $routeConfig[2], true);
138 1
            self::registerRoute($route->setTokens($routeConfig[1]));
139
        }
140 1
    }
141
142
    /**
143
     * Dispatch a request
144
     *
145
     * @param ServerRequestInterface $request
146
     * @return ResponseInterface $response
147
     */
148 2
    public static function dispatchRequest(ServerRequestInterface $request)
149
    {
150 2
        self::getServer()->dispatchRequest($request);
151 2
    }
152
153
    /**
154
     * Create and return the server instance
155
     *
156
     * @return \Apparat\Server\Domain\Model\Server Server instance
157
     */
158 2
    protected static function getServer()
159
    {
160 2
        if (self::$server === null) {
161 1
            self::$server = Kernel::create(\Apparat\Server\Domain\Model\Server::class);
162
        }
163 2
        return self::$server;
164
    }
165
166
    /**
167
     * Build and return the default date routes
168
     *
169
     * @param string $prefix Repository route prefix
170
     * @param int $precision Date precision
171
     * @return array Default date routes
172
     */
173 1
    protected static function buildDefaultDateRoutes($prefix, $precision)
174
    {
175 1
        return array_slice(
176
            [
177
                Route::SECOND => [
178 1
                    $prefix.'/{year}/{month}/{day}/{hour}/{minute}/{second}',
179 1
                    self::$TOKEN_YEAR +
180 1
                    self::$TOKEN_MONTH +
181 1
                    self::$TOKEN_DAY +
182 1
                    self::$TOKEN_HOUR +
183 1
                    self::$TOKEN_MINUTE +
184 1
                    self::$TOKEN_SECOND,
185
                    SecondAction::class
186 1
                ],
187
                Route::MINUTE => [
188 1
                    $prefix.'/{year}/{month}/{day}/{hour}/{minute}',
189 1
                    self::$TOKEN_YEAR +
190 1
                    self::$TOKEN_MONTH +
191 1
                    self::$TOKEN_DAY +
192 1
                    self::$TOKEN_HOUR +
193 1
                    self::$TOKEN_MINUTE,
194
                    MinuteAction::class
195
                ],
196
                Route::HOUR => [
197 1
                    $prefix.'/{year}/{month}/{day}/{hour}',
198 1
                    self::$TOKEN_YEAR +
199 1
                    self::$TOKEN_MONTH +
200 1
                    self::$TOKEN_DAY +
201 1
                    self::$TOKEN_HOUR,
202
                    HourAction::class
203
                ],
204
                Route::DAY => [
205 1
                    $prefix.'/{year}/{month}/{day}',
206 1
                    self::$TOKEN_YEAR +
207 1
                    self::$TOKEN_MONTH +
208 1
                    self::$TOKEN_DAY,
209
                    DayAction::class
210
                ],
211
                Route::MONTH => [
212 1
                    $prefix.'/{year}/{month}',
213 1
                    self::$TOKEN_YEAR +
214 1
                    self::$TOKEN_MONTH,
215
                    MonthAction::class
216
                ],
217
                Route::YEAR => [
218 1
                    $prefix.'/{year}',
219 1
                    self::$TOKEN_YEAR,
220
                    YearAction::class
221
                ]
222
            ],
223 1
            6 - $precision
224
        );
225
    }
226
227
    /**
228
     * Build and return the default object routes
229
     *
230
     * @param string $prefix Repository route prefix
231
     * @param array $baseDateRoute Base date route
232
     * @param int $numDefaultRoutes Total number of date routes
233
     * @return array Default object routes
234
     */
235 1
    protected static function buildDefaultObjectRoutes($prefix, $baseDateRoute, $numDefaultRoutes)
0 ignored issues
show
Unused Code introduced by
The parameter $prefix is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
236
    {
237
        // Build a regular expression for all supported object types
238 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...
239
240
        // Build the default object route
241
        $objectRoute = [
242 1
            $baseDateRoute[0].'/{hidden}{id}{type}{draft}{revision}{format}',
243 1
            $baseDateRoute[1] + [
244 1
                'hidden' => '\.?',
245 1
                'id' => self::REGEX_ASTERISK.'|(?:\d+)',
246 1
                'type' => $enabledObjectTypes.'?',
247 1
                'draft' => '(?:/(\.)?\\'.(2 + $numDefaultRoutes).')?',
248 1
                'revision' => '(?('.(5 + $numDefaultRoutes).')|(?:-\d+)?)',
249 1
                'format' => '(?('.(4 + $numDefaultRoutes).')(?:\.'.preg_quote(getenv('OBJECT_RESOURCE_EXTENSION')).')?)',
250
            ],
251
            ObjectAction::class
252
        ];
253 1
        return [Route::OBJECT => $objectRoute];
254
    }
255
}
256