Completed
Push — master ( 6a003f...20fceb )
by Joschi
02:53
created

Server::setViewResources()   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\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\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\ObjectsAction;
46
use Apparat\Server\Infrastructure\Action\SecondAction;
47
use Apparat\Server\Infrastructure\Action\TypeAction;
48
use Apparat\Server\Infrastructure\Action\YearAction;
49
use Apparat\Server\Infrastructure\Route\AbstractActionRoute;
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
     * View resources
65
     *
66
     * @var array
67
     */
68
    protected $viewResources = [];
69
70
    /**
71
     * Enable the object route for a particular repository
72
     *
73
     * @param string $repositoryPath Repository path
74
     * @param int $enable Enable / disable default actions
75
     */
76 1
    public function enableObjectRoute($repositoryPath = '', $enable = ObjectRoute::ALL)
77
    {
78 1
        $routeName = ObjectRoute::OBJECT_STR;
79 1
        $datePrecision = intval(getenv('OBJECT_DATE_PRECISION'));
80 1
        $selectorRegex = SelectorFactory::getSelectorRegex($datePrecision);
81 1
        $objectRouteActions = array_filter(
82 1
            array_merge(
83 1
                self::getEnabledObjectActionClasses($enable),
84 1
                self::getEnabledDateActionClasses($enable, $datePrecision)
85 1
            )
86 1
        );
87
88
        // Add a repository sub-pattern
89 1
        if (strlen($repositoryPath)) {
90 1
            $selectorRegex = '(?:/(?P<repository>.+?))'.$selectorRegex;
91 1
            $routeName .= '.'.$repositoryPath;
92 1
        }
93
94 1
        $route = new Route(Route::GET, $routeName, $selectorRegex, $objectRouteActions);
95 1
        $route->setAccepts(ObjectRoute::$accept);
96 1
        $route->setObject(true);
97 1
        $this->registerRoute($route);
98 1
    }
99
100
    /**
101
     * Return the enabled object action classes
102
     *
103
     * @param int $enable Enable / disable default actions
104
     * @return array Enabled object action classes
105
     */
106 1
    protected function getEnabledObjectActionClasses($enable)
107
    {
108
        return [
109 1
            ObjectRoute::OBJECT_STR => (ObjectRoute::OBJECT & $enable) ? ObjectAction::class : null,
110 1
            ObjectRoute::TYPE_STR => (ObjectRoute::TYPE & $enable) ? TypeAction::class : null,
111 1
            ObjectRoute::OBJECTS_STR => (ObjectRoute::OBJECTS & $enable) ? ObjectsAction::class : null,
112 1
        ];
113
    }
114
115
    /**
116
     * Return the enabled date action classes
117
     *
118
     * @param int $enable Enable / disable default actions
119
     * @param int $datePrecision Date precision
120
     * @return array Enabled date action classes
121
     */
122 1
    protected function getEnabledDateActionClasses($enable, $datePrecision)
123
    {
124 1
        return array_slice(
125
            [
126 1
                ObjectRoute::SECOND_STR => (ObjectRoute::SECOND & $enable) ? SecondAction::class : null,
127 1
                ObjectRoute::MINUTE_STR => (ObjectRoute::MINUTE & $enable) ? MinuteAction::class : null,
128 1
                ObjectRoute::HOUR_STR => (ObjectRoute::HOUR & $enable) ? HourAction::class : null,
129 1
                ObjectRoute::DAY_STR => (ObjectRoute::DAY & $enable) ? DayAction::class : null,
130 1
                ObjectRoute::MONTH_STR => (ObjectRoute::MONTH & $enable) ? MonthAction::class : null,
131 1
                ObjectRoute::YEAR_STR => (ObjectRoute::YEAR & $enable) ? YearAction::class : null,
132 1
            ],
133
            6 - $datePrecision
134 1
        );
135
    }
136
137
    /**
138
     * Prepare and return a route action
139
     *
140
     * @param ServerRequestInterface $request Request
141
     * @param AbstractActionRoute $route Route
142
     * @return ActionInterface|Callable $action Action
143
     */
144 80
    public function getRouteAction(ServerRequestInterface $request, AbstractActionRoute $route)
145
    {
146 80
        return $this->routerContainer->getRouteAction($request, $route);
147
    }
148
149
    /**
150
     * Return view resources
151
     *
152
     * @param string|null $name Optional: view resource name
153
     * @return array View resources
154
     */
155 78
    public function getViewResources($name = null)
156
    {
157 78
        return ($name === null) ?
158 78
            $this->viewResources :
159 78
            (array_key_exists($name, $this->viewResources) ? $this->viewResources[$name] : null);
160
    }
161
162
    /**
163
     * Set the view resources
164
     *
165
     * @param array $viewResources View resources
166
     */
167 2
    public function setViewResources(array $viewResources)
168
    {
169 2
        $this->viewResources = $viewResources;
170 2
    }
171
}
172