Completed
Push — master ( 893872...3f3cc0 )
by Joschi
02:50
created

Server::enableObjectRoute()   D

Complexity

Conditions 10
Paths 1

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
ccs 24
cts 24
cp 1
rs 4.8196
cc 10
eloc 19
nc 1
nop 2
crap 10

How to fix   Complexity   

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\Factory\SelectorFactory;
40
use Apparat\Server\Domain\Contract\ActionRouteInterface;
41
use Apparat\Server\Infrastructure\Action\DayAction;
42
use Apparat\Server\Infrastructure\Action\HourAction;
43
use Apparat\Server\Infrastructure\Action\MinuteAction;
44
use Apparat\Server\Infrastructure\Action\MonthAction;
45
use Apparat\Server\Infrastructure\Action\ObjectAction;
46
use Apparat\Server\Infrastructure\Action\ObjectsAction;
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\Action\ActionInterface;
51
use Apparat\Server\Ports\Route\Route;
52
use Apparat\Server\Ports\Types\ObjectRoute;
53
use Psr\Http\Message\ServerRequestInterface;
54
55
/**
56
 * Server instance
57
 *
58
 * @package Apparat\Server
59
 * @subpackage Apparat\Server\Infrastructure
60
 */
61
class Server extends \Apparat\Server\Domain\Model\Server
62
{
63
    /**
64
     * Enable the object route for a particular repository
65
     *
66
     * @param string $repositoryPath Repository path
67
     * @param int $enable Enable / disable default routes
68
     */
69 1
    public function enableObjectRoute($repositoryPath = '', $enable = ObjectRoute::ALL)
70
    {
71
        // Repository route prefix
72 1
        $prefix = rtrim('/'.$repositoryPath, '/');
73 1
        $datePrecision = intval(getenv('OBJECT_DATE_PRECISION'));
74 1
        $selectorRegex = SelectorFactory::getSelectorRegex($datePrecision);
75 1
        $objectRouteActions = array_filter(
76 1
            array_merge(
77
                [
78 1
                    ObjectRoute::OBJECT_STR => (ObjectRoute::OBJECT & $enable) ? ObjectAction::class : null,
79 1
                    ObjectRoute::TYPE_STR => (ObjectRoute::TYPE & $enable) ? TypeAction::class : null,
80 1
                    ObjectRoute::OBJECTS_STR => (ObjectRoute::OBJECTS & $enable) ? ObjectsAction::class : null,
81 1
                ],
82 1
                array_slice(
83
                    [
84 1
                        ObjectRoute::SECOND_STR => (ObjectRoute::SECOND & $enable) ? SecondAction::class : null,
85 1
                        ObjectRoute::MINUTE_STR => (ObjectRoute::MINUTE & $enable) ? MinuteAction::class : null,
86 1
                        ObjectRoute::HOUR_STR => (ObjectRoute::HOUR & $enable) ? HourAction::class : null,
87 1
                        ObjectRoute::DAY_STR => (ObjectRoute::DAY & $enable) ? DayAction::class : null,
88 1
                        ObjectRoute::MONTH_STR => (ObjectRoute::MONTH & $enable) ? MonthAction::class : null,
89 1
                        ObjectRoute::YEAR_STR => (ObjectRoute::YEAR & $enable) ? YearAction::class : null,
90 1
                    ],
91
                    6 - $datePrecision
92 1
                )
93 1
            )
94 1
        );
95
96 1
        $route = new Route(Route::GET, ObjectRoute::OBJECT_STR, $prefix.$selectorRegex, $objectRouteActions);
97 1
        $this->registerRoute($route);
98 1
    }
99
100
    /**
101
     * Prepare and return a route action
102
     *
103
     * @param ServerRequestInterface $request Request
104
     * @param ActionRouteInterface $route Route
105
     * @return ActionInterface|Callable $action Action
106
     */
107 70
    public function getRouteAction(ServerRequestInterface $request, ActionRouteInterface $route)
108
    {
109 70
        return $this->routerContainer->getRouteAction($request, $route);
110
    }
111
}
112