Completed
Push — feature/other-validation ( c4ebdf...a3189f )
by Narcotic
152:45 queued 146:40
created

ActionUtils   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 210
Duplicated Lines 6.67 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 14
loc 210
ccs 59
cts 59
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getRouteGet() 0 4 1
A getRoute() 0 20 2
A getBaseFromService() 0 15 3
A getRouteAll() 0 4 1
A getRoutePost() 0 4 1
A getRoutePut() 0 4 1
A getRoutePatch() 0 4 1
A getRouteDelete() 0 4 1
A getRouteOptions() 7 7 2
A getRouteHead() 7 7 2
A getCanonicalSchemaRoute() 0 22 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Generate routes for individual actions
4
 */
5
6
namespace Graviton\RestBundle\Routing\Loader;
7
8
use Symfony\Component\Routing\Route;
9
10
/**
11
 * Generate routes for individual actions
12
 *
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class ActionUtils
18
{
19
    const ID_PATTERN = '[a-zA-Z0-9\-_\/\+\040\'\.]+';
20
21
    /**
22
     * Get route for GET requests
23
     *
24
     * @param string $service       service id
25
     * @param array  $serviceConfig service configuration
26
     *
27
     * @return Route
28
     */
29 2
    public static function getRouteGet($service, $serviceConfig)
30
    {
31 2
        return self::getRoute($service, 'GET', 'getAction', $serviceConfig, array('id' => self::ID_PATTERN));
32
    }
33
34
    /**
35
     * Get Route
36
     *
37
     * @param string $service       name of service containing controller
38
     * @param string $method        HTTP method to generate route for
39
     * @param string $action        action to call for route
40
     * @param array  $serviceConfig service configuration
41
     * @param array  $parameters    route parameters to append to route as pair of name and patterns
42
     *
43
     * @return Route
44
     */
45 2
    private static function getRoute($service, $method, $action, $serviceConfig, $parameters = array())
46
    {
47 2
        $pattern = self::getBaseFromService($service, $serviceConfig);
48
        $defaults = array(
49 2
            '_controller' => $service . ':' . $action,
50 2
            '_format' => '~',
51 1
        );
52
53 2
        $requirements = [];
54
55 2
        foreach ($parameters as $paramName => $paramPattern) {
56 2
            $pattern .= '{' . $paramName . '}';
57 2
            $requirements[$paramName] = $paramPattern;
58 1
        }
59
60 2
        $route = new Route($pattern, $defaults, $requirements);
61 2
        $route->setMethods($method);
62
63 2
        return $route;
64
    }
65
66
    /**
67
     * Get entity name from service strings.
68
     *
69
     * By convention the last part of the service string so far
70
     * makes up the entities name.
71
     *
72
     * @param string $service       (partial) service id
73
     * @param array  $serviceConfig service configuration
74
     *
75
     * @return string
76
     */
77 2
    private static function getBaseFromService($service, $serviceConfig)
78
    {
79 2
        if (isset($serviceConfig[0]['router-base']) && strlen($serviceConfig[0]['router-base']) > 0) {
80 2
            $base = $serviceConfig[0]['router-base'] . '/';
81 1
        } else {
82 2
            $parts = explode('.', $service);
83
84 2
            $entity = array_pop($parts);
85 2
            $module = $parts[1];
86
87 2
            $base = '/' . $module . '/' . $entity . '/';
88
        }
89
90 2
        return $base;
91
    }
92
93
    /**
94
     * Get route for getAll requests
95
     *
96
     * @param string $service       service id
97
     * @param array  $serviceConfig service configuration
98
     *
99
     * @return Route
100
     */
101 2
    public static function getRouteAll($service, $serviceConfig)
102
    {
103 2
        return self::getRoute($service, 'GET', 'allAction', $serviceConfig);
104
    }
105
106
    /**
107
     * Get route for POST requests
108
     *
109
     * @param string $service       service id
110
     * @param array  $serviceConfig service configuration
111
     *
112
     * @return Route
113
     */
114 2
    public static function getRoutePost($service, $serviceConfig)
115
    {
116 2
        return self::getRoute($service, 'POST', 'postAction', $serviceConfig);
117
    }
118
119
    /**
120
     * Get route for PUT requests
121
     *
122
     * @param string $service       service id
123
     * @param array  $serviceConfig service configuration
124
     *
125
     * @return Route
126
     */
127 2
    public static function getRoutePut($service, $serviceConfig)
128
    {
129 2
        return self::getRoute($service, 'PUT', 'putAction', $serviceConfig, array('id' => self::ID_PATTERN));
130
    }
131
132
    /**
133
     * Get route for PATCH requests
134
     *
135
     * @param string $service       service id
136
     * @param array  $serviceConfig service configuration
137
     *
138
     * @return Route
139
     */
140 2
    public static function getRoutePatch($service, $serviceConfig)
141
    {
142 2
        return self::getRoute($service, 'PATCH', 'patchAction', $serviceConfig, array('id' => self::ID_PATTERN));
143
    }
144
145
    /**
146
     * Get route for DELETE requests
147
     *
148
     * @param string $service       service id
149
     * @param array  $serviceConfig service configuration
150
     *
151
     * @return Route
152
     */
153 2
    public static function getRouteDelete($service, $serviceConfig)
154
    {
155 2
        return self::getRoute($service, 'DELETE', 'deleteAction', $serviceConfig, array('id' => self::ID_PATTERN));
156
    }
157
158
    /**
159
     * Get route for OPTIONS requests
160
     *
161
     * @param string  $service       service id
162
     * @param array   $serviceConfig service configuration
163
     * @param array   $parameters    service params
164
     * @param boolean $useIdPattern  generate route with id param
165
     *
166
     * @return Route
167
     */
168 2 View Code Duplication
    public static function getRouteOptions($service, $serviceConfig, array $parameters = array(), $useIdPattern = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170 2
        if ($useIdPattern) {
171 2
            $parameters['id'] = self::ID_PATTERN;
172 1
        }
173 2
        return self::getRoute($service, 'OPTIONS', 'optionsAction', $serviceConfig, $parameters);
174
    }
175
176
    /**
177
     * Get route for HEAD requests
178
     *
179
     * @param string  $service       service id
180
     * @param array   $serviceConfig service configuration
181
     * @param array   $parameters    service params
182
     * @param boolean $useIdPattern  generate route with id param
183
     *
184
     * @return Route
185
     */
186 2 View Code Duplication
    public static function getRouteHead($service, $serviceConfig, array $parameters = array(), $useIdPattern = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188 2
        if ($useIdPattern) {
189 2
            $parameters['id'] = self::ID_PATTERN;
190 1
        }
191 2
        return self::getRoute($service, 'HEAD', 'optionsAction', $serviceConfig, $parameters);
192
    }
193
194
    /**
195
     * Get canonical route for schema requests
196
     *
197
     * @param string  $service       service id
198
     * @param array   $serviceConfig service configuration
199
     * @param string  $type          service type (item or collection)
200
     * @param boolean $option        render a options route
201
     *
202
     * @return Route
203
     */
204 2
    public static function getCanonicalSchemaRoute($service, $serviceConfig, $type = 'item', $option = false)
205
    {
206 2
        $pattern = self::getBaseFromService($service, $serviceConfig);
207 2
        $pattern = '/schema' . $pattern . $type;
208
209 2
        $action = 'schemaAction';
210 2
        $method = 'GET';
211 2
        if ($option !== false) {
212 2
            $action = 'optionsAction';
213 2
            $method = 'OPTIONS';
214 1
        }
215
216
        $defaults = array(
217 2
            '_controller' => $service . ':' . $action,
218 2
            '_format' => '~',
219 1
        );
220
221 2
        $route = new Route($pattern, $defaults, []);
222 2
        $route->setMethods($method);
223
224 2
        return $route;
225
    }
226
}
227