Completed
Push — master ( 819036...d88ff8 )
by Joschi
08:13
created

Server::buildDefaultDateRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 1

Importance

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