FluteRoutesTrait   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 194
Duplicated Lines 22.16 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 43
loc 194
c 0
b 0
f 0
wmc 22
lcom 0
cbo 1
ccs 64
cts 64
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B apiController() 3 36 6
B webController() 16 45 8
A relationship() 0 16 1
A addInRelationship() 12 12 1
A removeInRelationship() 12 12 1
A routeName() 0 16 5

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 declare (strict_types = 1);
2
3
namespace Limoncello\Flute\Http\Traits;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Contracts\Routing\GroupInterface;
22
use Limoncello\Contracts\Routing\RouteInterface;
23
use Limoncello\Flute\Contracts\Http\Controller\ControllerCreateInterface;
24
use Limoncello\Flute\Contracts\Http\Controller\ControllerDeleteInterface;
25
use Limoncello\Flute\Contracts\Http\Controller\ControllerIndexInterface;
26
use Limoncello\Flute\Contracts\Http\Controller\ControllerInstanceInterface;
27
use Limoncello\Flute\Contracts\Http\Controller\ControllerReadInterface;
28
use Limoncello\Flute\Contracts\Http\Controller\ControllerUpdateInterface;
29
use Limoncello\Flute\Contracts\Http\JsonApiControllerInterface as JCI;
30
use Limoncello\Flute\Contracts\Http\WebControllerInterface as FCI;
31
use Neomerx\JsonApi\Contracts\Schema\DocumentInterface;
32
use function assert;
33
use function class_exists;
34
use function class_implements;
35
use function in_array;
36
use function substr;
37
38
/**
39
 * @package Limoncello\Flute
40
 */
41
trait FluteRoutesTrait
42
{
43
    /**
44
     * @param GroupInterface $group
45 1
     * @param string         $resourceName
46
     * @param string         $controllerClass
47
     *
48
     * @return GroupInterface
49
     */
50 1
    protected static function apiController(
51
        GroupInterface $group,
52 1
        string $resourceName,
53 1
        string $controllerClass
54
    ): GroupInterface {
55 1
        assert(class_exists($controllerClass) === true);
56 1
57
        $groupPrefix = $group->getUriPrefix();
58 1
        $indexSlug   = '/{' . JCI::ROUTE_KEY_INDEX . '}';
59 1
        $params      = function (string $method) use ($groupPrefix, $resourceName): array {
60
            return [RouteInterface::PARAM_NAME => static::routeName($groupPrefix, $resourceName, $method)];
61
        };
62 1
        $handler     = function (string $method) use ($controllerClass): array {
63 1
            return [$controllerClass, $method];
64 1
        };
65
66 1
        // if the class implements any of CRUD methods a corresponding route will be added
67 1
        $classInterfaces = class_implements($controllerClass);
68
        if (in_array(ControllerIndexInterface::class, $classInterfaces) === true) {
69 1
            $group->get($resourceName, $handler(JCI::METHOD_INDEX), $params(JCI::METHOD_INDEX));
70 1
        }
71
        if (in_array(ControllerCreateInterface::class, $classInterfaces) === true) {
72 1
            $group->post($resourceName, $handler(JCI::METHOD_CREATE), $params(JCI::METHOD_CREATE));
73 1
        }
74
        if (in_array(ControllerReadInterface::class, $classInterfaces) === true) {
75 1
            $group->get($resourceName . $indexSlug, $handler(JCI::METHOD_READ), $params(JCI::METHOD_READ));
76 1
        }
77
        if (in_array(ControllerUpdateInterface::class, $classInterfaces) === true) {
78
            $group->patch($resourceName . $indexSlug, $handler(JCI::METHOD_UPDATE), $params(JCI::METHOD_UPDATE));
79 1
        }
80 View Code Duplication
        if (in_array(ControllerDeleteInterface::class, $classInterfaces) === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
81
            $group->delete($resourceName . $indexSlug, $handler(JCI::METHOD_DELETE), $params(JCI::METHOD_DELETE));
82
        }
83
84
        return $group;
85
    }
86
87
    /**
88
     * @param GroupInterface $group
89
     * @param string         $subUri
90 1
     * @param string         $controllerClass
91
     * @param string         $createSubUrl
92
     *
93
     * @return GroupInterface
94
     */
95
    protected static function webController(
96
        GroupInterface $group,
97 1
        string $subUri,
98 1
        string $controllerClass,
99
        string $createSubUrl = '/create'
100
    ): GroupInterface {
101 1
        // normalize url to have predictable URLs and their names
102 1
        if ($subUri[-1] === '/') {
103
            $subUri = substr($subUri, 0, -1);
104 1
        }
105 1
106
        $groupPrefix = $group->getUriPrefix();
107 1
        $slugged     = $subUri . '/{' . FCI::ROUTE_KEY_INDEX . '}';
108 1
        $params      = function (string $method) use ($groupPrefix, $subUri) : array {
109
            return [RouteInterface::PARAM_NAME => static::routeName($groupPrefix, $subUri, $method)];
110
        };
111
        $handler     = function (string $method) use ($controllerClass): array {
112 1
            return [$controllerClass, $method];
113 1
        };
114 1
115
        // if the class implements any of CRUD methods a corresponding route will be added
116 1
        // as HTML forms do not support methods other than GET/POST we use POST and special URI for update and delete.
117 1
        $classInterfaces = class_implements($controllerClass);
118
        if (in_array(ControllerIndexInterface::class, $classInterfaces) === true) {
119 1
            $group->get($subUri, $handler(FCI::METHOD_INDEX), $params(FCI::METHOD_INDEX));
120 1
        }
121 View Code Duplication
        if (in_array(ControllerInstanceInterface::class, $classInterfaces) === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
122 1
            $group->get($subUri . $createSubUrl, $handler(FCI::METHOD_INSTANCE), $params(FCI::METHOD_INSTANCE));
123 1
        }
124 View Code Duplication
        if (in_array(ControllerCreateInterface::class, $classInterfaces) === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
125 1
            $group->post($subUri . $createSubUrl, $handler(FCI::METHOD_CREATE), $params(FCI::METHOD_CREATE));
126 1
        }
127 View Code Duplication
        if (in_array(ControllerReadInterface::class, $classInterfaces) === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
128 1
            $group->get($slugged, $handler(FCI::METHOD_READ), $params(FCI::METHOD_READ));
129 1
        }
130 1 View Code Duplication
        if (in_array(ControllerUpdateInterface::class, $classInterfaces) === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
131
            $group->post($slugged, $handler(FCI::METHOD_UPDATE), $params(FCI::METHOD_UPDATE));
132
        }
133 1 View Code Duplication
        if (in_array(ControllerDeleteInterface::class, $classInterfaces) === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
134
            $deleteUri = $slugged . '/' . FCI::METHOD_DELETE;
135
            $group->post($deleteUri, $handler(FCI::METHOD_DELETE), $params(FCI::METHOD_DELETE));
136
        }
137
138
        return $group;
139
    }
140
141
    /**
142
     * @param GroupInterface $group
143
     * @param string         $resourceName
144
     * @param string         $relationshipName
145 1
     * @param string         $controllerClass
146
     * @param string         $selfGetMethod
147
     *
148
     * @return GroupInterface
149
     */
150
    protected static function relationship(
151
        GroupInterface $group,
152 1
        string $resourceName,
153 1
        string $relationshipName,
154
        string $controllerClass,
155
        string $selfGetMethod
156
    ): GroupInterface {
157 1
        $resourceIdUri = $resourceName . '/{' . JCI::ROUTE_KEY_INDEX . '}/';
158
        $selfUri       = $resourceIdUri . DocumentInterface::KEYWORD_RELATIONSHIPS . '/' . $relationshipName;
159 1
160
        return $group
161
            // `self`
162
            ->get($selfUri, [$controllerClass, $selfGetMethod])
163
            // `related`
164
            ->get($resourceIdUri . $relationshipName, [$controllerClass, $selfGetMethod]);
165
    }
166
167
    /**
168
     * @param GroupInterface $group
169
     * @param string         $resourceName
170
     * @param string         $relationshipName
171 1
     * @param string         $controllerClass
172
     * @param string         $addMethod
173
     *
174
     * @return GroupInterface
175
     */
176 View Code Duplication
    protected static function addInRelationship(
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...
177
        GroupInterface $group,
178 1
        string $resourceName,
179 1
        string $relationshipName,
180
        string $controllerClass,
181 1
        string $addMethod
182
    ): GroupInterface {
183
        $url = $resourceName . '/{' . JCI::ROUTE_KEY_INDEX . '}/' .
184
            DocumentInterface::KEYWORD_RELATIONSHIPS . '/' . $relationshipName;
185
186
        return $group->post($url, [$controllerClass, $addMethod]);
187
    }
188
189
    /**
190
     * @param GroupInterface $group
191
     * @param string         $resourceName
192
     * @param string         $relationshipName
193 1
     * @param string         $controllerClass
194
     * @param string         $deleteMethod
195
     *
196
     * @return GroupInterface
197
     */
198 View Code Duplication
    protected static function removeInRelationship(
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...
199
        GroupInterface $group,
200 1
        string $resourceName,
201 1
        string $relationshipName,
202
        string $controllerClass,
203 1
        string $deleteMethod
204
    ): GroupInterface {
205
        $url = $resourceName . '/{' . JCI::ROUTE_KEY_INDEX . '}/' .
206
            DocumentInterface::KEYWORD_RELATIONSHIPS . '/' . $relationshipName;
207
208
        return $group->delete($url, [$controllerClass, $deleteMethod]);
209
    }
210
211
    /**
212
     * @param string $prefix
213 3
     * @param string $subUri
214
     * @param string $method
215 3
     *
216
     * @return string
217
     */
218
    protected static function routeName(string $prefix, string $subUri, string $method): string
219 3
    {
220 3
        assert(empty($method) === false);
221
222
        // normalize prefix and url to have predictable name
223 3
224 1
        if (empty($prefix) === true || $prefix[-1] !== '/') {
225
            $prefix .= '/';
226
        }
227 3
228
        if (empty($subUri) === false && $subUri[-1] === '/') {
229
            $subUri = substr($subUri, 0, -1);
230
        }
231
232
        return $prefix . $subUri . '::' . $method;
233
    }
234
}
235