Registry::getServiceAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.032
1
<?php
2
/**
3
 * This file is part of the Drest package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author Lee Davis
9
 * @copyright Copyright (c) Lee Davis <@leedavis81>
10
 * @link https://github.com/leedavis81/drest/blob/master/LICENSE
11
 * @license http://opensource.org/licenses/MIT The MIT X License (MIT)
12
 */
13
namespace Drest\Service\Action;
14
15
use Drest\DrestException;
16
17
/**
18
 * Service action registry class
19
 * This is used to register custom service actions
20
 *
21
 */
22
class Registry
23
{
24
    /**
25
     * An array of action objects
26
     * @var array $actions
27
     */
28
    protected $actions = [];
29
30
    /**
31
     * An array of routes using the name as key, and corresponding offset (int) in $actions array
32
     * @var array $routes
33
     * - example ['User::get_user' => 0, 'User::get_user' => 0]
34
     */
35
    protected $routes = [];
36
37
    /**
38
     * Register service actions by their named route.
39
     * E.g. ->register($action, ['User::post_user', 'Address::post_address'])
40
     * @param AbstractAction $action
41
     * @param array $namedRoutes
42
     * @throws DrestException
43
     */
44 4
    public function register(AbstractAction $action, array $namedRoutes)
45
    {
46 4
        $this->actions[] = $action;
47 4
        $key = array_search($action, $this->actions);
48 4
        foreach ($namedRoutes as $route)
49
        {
50
            // Check the format
51 4
            $this->checkNamedRoute($route);
52 4
            $this->routes[$route] = $key;
53 4
        }
54 4
    }
55
56
    /**
57
     * Check the format of the named route
58
     * @param $namedRoute
59
     * @throws DrestException
60
     */
61 4
    protected function checkNamedRoute($namedRoute)
62
    {
63 4
        if (substr_count($namedRoute, '::') !== 1) {
64
            throw DrestException::invalidNamedRouteSyntax();
65
        }
66 4
        if (sizeof(explode('::', $namedRoute)) !== 2)
67 4
        {
68
            throw DrestException::invalidNamedRouteSyntax();
69
        }
70 4
    }
71
72
    /**
73
     * Unregister by action object
74
     * @param AbstractAction $action
75
     */
76 1
    public function unregisterByAction(AbstractAction $action)
77
    {
78 1
        if (($offset = array_search($action, $this->actions)) !== false)
79 1
        {
80 1
            unset($this->actions[$offset]);
81 1
            foreach ($this->routes as $key => $value)
82
            {
83 1
                if ($value === $offset)
84 1
                {
85
                    // Remove any routes registered with this action
86 1
                    unset($this->routes[$key]);
87 1
                }
88 1
            }
89 1
        }
90 1
    }
91
92
    /**
93
     * Unregister an action
94
     * @param \Drest\Mapping\RouteMetaData|string $route
95
     * - can either be a route metadata object, or a string 'User::get_user'
96
     */
97 2
    public function unregisterByRoute($route)
98
    {
99 2
        $routeName = ($route instanceof \Drest\Mapping\RouteMetaData)
100 2
            ? $route->getNamedRoute()
101 2
            : $route;
102
103 2
        $this->checkNamedRoute($routeName);
104
105 2
        if (isset($this->routes[$routeName]))
106 2
        {
107 2
            $actionOffset = $this->routes[$routeName];
108 2
            unset($this->routes[$routeName]);
109
110 2
            if (!in_array($actionOffset, $this->routes))
111 2
            {
112
                // It's no longer in the routes array, so we should remove it from actions
113 2
                unset($this->actions[$actionOffset]);
114 2
            }
115 2
        }
116 2
    }
117
118
    /**
119
     * Does this registry have a service action for this route
120
     * @param \Drest\Mapping\RouteMetaData $routeMetaData
121
     * @return bool
122
     */
123 30
    public function hasServiceAction(\Drest\Mapping\RouteMetaData $routeMetaData)
124
    {
125 30
        return array_key_exists($routeMetaData->getNamedRoute(), $this->routes);
126
    }
127
128
    /**
129
     * Get the service action class
130
     * @param \Drest\Mapping\RouteMetaData $routeMetaData
131
     * @return AbstractAction|null
132
     */
133 1
    public function getServiceAction(\Drest\Mapping\RouteMetaData $routeMetaData)
134
    {
135 1
        if (!$this->hasServiceAction($routeMetaData))
136 1
        {
137
            return null;
138
        }
139 1
        return $this->actions[$this->routes[$routeMetaData->getNamedRoute()]];
140
    }
141
142
}