Completed
Push — master ( aea678...819036 )
by Joschi
04:14
created

ServerFacade::buildDefaultDateRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 53
ccs 31
cts 31
cp 1
rs 9.5797
cc 1
eloc 42
nc 1
nop 2
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Facade;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Ports\Object;
41
use Apparat\Server\Domain\Contract\RouteInterface;
42
use Apparat\Server\Infrastructure\Action\DayAction;
43
use Apparat\Server\Infrastructure\Action\HourAction;
44
use Apparat\Server\Infrastructure\Action\MinuteAction;
45
use Apparat\Server\Infrastructure\Action\MonthAction;
46
use Apparat\Server\Infrastructure\Action\ObjectAction;
47
use Apparat\Server\Infrastructure\Action\SecondAction;
48
use Apparat\Server\Infrastructure\Action\TypeAction;
49
use Apparat\Server\Infrastructure\Action\YearAction;
50
use Apparat\Server\Ports\Route\Route;
51
use Psr\Http\Message\ResponseInterface;
52
use Psr\Http\Message\ServerRequestInterface;
53
54
/**
55
 * Server facade
56
 *
57
 * @package Apparat\Server\Ports
58
 */
59
class ServerFacade
60
{
61
    /**
62
     * Asterisk regular expression
63
     *
64
     * @var string
65
     */
66
    const REGEX_ASTERISK = '(?:\%2A)';
67
    /**
68
     * Server instance
69
     *
70
     * @var \Apparat\Server\Domain\Model\Server
71
     */
72
    protected static $server = null;
73
    /**
74
     * Year route token
75
     *
76
     * @var array
77
     */
78
    protected static $TOKEN_YEAR = ['year' => self::REGEX_ASTERISK.'|(?:\d{4})'];
79
    /**
80
     * Month route token
81
     *
82
     * @var array
83
     */
84
    protected static $TOKEN_MONTH = ['month' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:1[0-2])'];
85
    /**
86
     * Day route token
87
     *
88
     * @var array
89
     */
90
    protected static $TOKEN_DAY = ['day' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:[1-2]\d)|(?:3[0-1])'];
91
    /**
92
     * Hour route token
93
     *
94
     * @var array
95
     */
96
    protected static $TOKEN_HOUR = ['hour' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:1[0-2])'];
97
    /**
98
     * Day route token
99
     *
100
     * @var array
101
     */
102
    protected static $TOKEN_MINUTE = ['minute' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:[1-4]\d)|(?:5[0-9])'];
103
    /**
104
     * Second route token
105
     *
106
     * @var array
107
     */
108
    protected static $TOKEN_SECOND = ['second' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:[1-4]\d)|(?:5[0-9])'];
109
110
    /**
111
     * Register the default routes for a particular repository
112
     *
113
     * @param string $repositoryPath Repository path
114
     * @param bool $enable Enable / disable default routes
115
     */
116 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...
117
    {
118
        // Repository route prefix
119 1
        $prefix = rtrim('/'.$repositoryPath, '/');
120
121
        // Build the list of default routes
122 1
        $defaultDateRoutes = self::buildDefaultDateRoutes($prefix, getenv('OBJECT_DATE_PRECISION'));
123 1
        $baseDateRoute = count($defaultDateRoutes) ? current($defaultDateRoutes) : ['/', []];
124 1
        $defaultObjectRoutes = self::buildDefaultObjectRoutes($prefix, $baseDateRoute, count($defaultDateRoutes));
125 1
        $defaultRoutes = $defaultObjectRoutes + $defaultDateRoutes;
126
127
        // Iterate through and register all base routes
128 1
        foreach ($defaultRoutes as $routeName => $routeConfig) {
129 1
            $route = new Route(Route::GET, $routeName, $routeConfig[0], $routeConfig[2], true);
130 1
            self::registerRoute($route->setTokens($routeConfig[1]));
131
        }
132 1
    }
133
134
    /**
135
     * Build and return the default date routes
136
     *
137
     * @param string $prefix Repository route prefix
138
     * @param int $precision Date precision
139
     * @return array Default date routes
140
     */
141 1
    protected static function buildDefaultDateRoutes($prefix, $precision)
142
    {
143 1
        return array_slice(
144
            [
145
                Route::SECOND => [
146 1
                    $prefix.'/{year}/{month}/{day}/{hour}/{minute}/{second}',
147 1
                    self::$TOKEN_YEAR +
148 1
                    self::$TOKEN_MONTH +
149 1
                    self::$TOKEN_DAY +
150 1
                    self::$TOKEN_HOUR +
151 1
                    self::$TOKEN_MINUTE +
152 1
                    self::$TOKEN_SECOND,
153
                    SecondAction::class
154 1
                ],
155
                Route::MINUTE => [
156 1
                    $prefix.'/{year}/{month}/{day}/{hour}/{minute}',
157 1
                    self::$TOKEN_YEAR +
158 1
                    self::$TOKEN_MONTH +
159 1
                    self::$TOKEN_DAY +
160 1
                    self::$TOKEN_HOUR +
161 1
                    self::$TOKEN_MINUTE,
162
                    MinuteAction::class
163
                ],
164
                Route::HOUR => [
165 1
                    $prefix.'/{year}/{month}/{day}/{hour}',
166 1
                    self::$TOKEN_YEAR +
167 1
                    self::$TOKEN_MONTH +
168 1
                    self::$TOKEN_DAY +
169 1
                    self::$TOKEN_HOUR,
170
                    HourAction::class
171
                ],
172
                Route::DAY => [
173 1
                    $prefix.'/{year}/{month}/{day}',
174 1
                    self::$TOKEN_YEAR +
175 1
                    self::$TOKEN_MONTH +
176 1
                    self::$TOKEN_DAY,
177
                    DayAction::class
178
                ],
179
                Route::MONTH => [
180 1
                    $prefix.'/{year}/{month}',
181 1
                    self::$TOKEN_YEAR +
182 1
                    self::$TOKEN_MONTH,
183
                    MonthAction::class
184
                ],
185
                Route::YEAR => [
186 1
                    $prefix.'/{year}',
187 1
                    self::$TOKEN_YEAR,
188
                    YearAction::class
189
                ]
190
            ],
191 1
            6 - $precision
192
        );
193
    }
194
195
    /**
196
     * Build and return the default object routes
197
     *
198
     * @param string $prefix Repository route prefix
199
     * @param array $baseDateRoute Base date route
200
     * @param int $numDefaultRoutes Total number of date routes
201
     * @return array Default object routes
202
     */
203 1
    protected static function buildDefaultObjectRoutes($prefix, $baseDateRoute, $numDefaultRoutes)
204
    {
205
        // Build a regular expression for all supported object types
206
        $enabledObjectTypes = '(?:-(?:(?:'.
207 1
            implode(')|(?:', array_map('preg_quote', \Apparat\Object\Ports\Types\Object::getSupportedTypes())).')))';
208
209
        return [
210
            // Default object route
211
            Route::OBJECT => [
212 1
                $prefix.$baseDateRoute[0].'/{hidden}{id}{type}{draft}{revision}{format}',
213 1
                $baseDateRoute[1] + [
214 1
                    'hidden' => '\.?',
215 1
                    'id' => '\d+',
216 1
                    'type' => $enabledObjectTypes.'?',
217 1
                    'draft' => '(?:/(\.)?\\'.(2 + $numDefaultRoutes).')?',
218 1
                    'revision' => '(?('.(5 + $numDefaultRoutes).')|(?:-\d+)?)',
219 1
                    'format' => '(?('.(4 + $numDefaultRoutes).')(?:\.'.preg_quote(getenv('OBJECT_RESOURCE_EXTENSION')).
220 1
                        ')?)',
221
                ],
222
                ObjectAction::class
223 1
            ],
224
            // Default type route
225
            Route::TYPE => [
226 1
                $baseDateRoute[0].'/{hidden}{id}{type}{draft}{revision}{format}',
227 1
                $baseDateRoute[1] + [
228 1
                    'hidden' => '\.?',
229 1
                    'id' => self::REGEX_ASTERISK,
230 1
                    'type' => $enabledObjectTypes.'?',
231 1
                    'draft' => '(?:/(\.)?'.self::REGEX_ASTERISK.')?',
232 1
                    'revision' => '(?('.(5 + $numDefaultRoutes).')|(?:-\d+)?)',
233 1
                    'format' => '(?('.(4 + $numDefaultRoutes).')(?:\.'.preg_quote(getenv('OBJECT_RESOURCE_EXTENSION')).
234 1
                        ')?)',
235
                ],
236
                TypeAction::class
237
            ]
238
        ];
239
    }
240
241
    /**
242
     * Register a route
243
     *
244
     * @param RouteInterface $route
245
     */
246 2
    public static function registerRoute(RouteInterface $route)
247
    {
248 2
        self::getServer()->registerRoute($route);
249 2
    }
250
251
    /**
252
     * Create and return the server instance
253
     *
254
     * @return \Apparat\Server\Domain\Model\Server Server instance
255
     */
256 2
    protected static function getServer()
257
    {
258 2
        if (self::$server === null) {
259 1
            self::$server = Kernel::create(\Apparat\Server\Domain\Model\Server::class);
260
        }
261 2
        return self::$server;
262
    }
263
264
    /**
265
     * Dispatch a request
266
     *
267
     * @param ServerRequestInterface $request
268
     * @return ResponseInterface $response
269
     */
270 2
    public static function dispatchRequest(ServerRequestInterface $request)
271
    {
272 2
        return self::getServer()->dispatchRequest($request);
273
    }
274
}
275