Completed
Push — develop ( d26178...6d3477 )
by Neomerx
04:43
created

FluteRoutesTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 13.39 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 1
dl 17
loc 127
ccs 58
cts 58
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B resource() 9 42 6
B controller() 8 33 6
B relationship() 0 26 1

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 namespace Limoncello\Flute\Http\Traits;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Contracts\Routing\GroupInterface;
20
use Limoncello\Contracts\Routing\RouteInterface;
21
use Limoncello\Flute\Contracts\Http\Controller\ControllerCreateInterface;
22
use Limoncello\Flute\Contracts\Http\Controller\ControllerDeleteInterface;
23
use Limoncello\Flute\Contracts\Http\Controller\ControllerIndexInterface;
24
use Limoncello\Flute\Contracts\Http\Controller\ControllerReadInterface;
25
use Limoncello\Flute\Contracts\Http\Controller\ControllerUpdateInterface;
26
use Limoncello\Flute\Contracts\Http\ControllerInterface as CI;
27
use Limoncello\Flute\Contracts\Schema\SchemaInterface;
28
use Limoncello\Flute\Http\BaseController;
29
use Neomerx\JsonApi\Contracts\Document\DocumentInterface;
30
31
/**
32
 * @package Limoncello\Flute
33
 */
34
trait FluteRoutesTrait
35
{
36
    /**
37
     * @param GroupInterface $group
38
     * @param string         $controllerClass
39
     *
40
     * @return GroupInterface
41
     */
42 1
    protected static function resource(GroupInterface $group, string $controllerClass): GroupInterface
43
    {
44
        /** @var BaseController $controllerClass */
45 1
        assert(array_key_exists(BaseController::class, class_parents($controllerClass)) === true);
46 1
        $schemeClass = $controllerClass::SCHEMA_CLASS;
47 1
        assert(
48 1
            empty($schemeClass) === false,
49 1
            "Scheme class is not specified for controller `$controllerClass`."
50
        );
51
52
        /** @var SchemaInterface $schemeClass */
53 1
        assert(array_key_exists(SchemaInterface::class, class_implements($schemeClass)) === true);
54 1
        $type = $schemeClass::TYPE;
55
56 1
        $indexSlug = '/{' . BaseController::ROUTE_KEY_INDEX . '}';
57 1
        $params    = function ($method) use ($type) {
58 1
            return [RouteInterface::PARAM_NAME => $type . '_' . $method];
59 1
        };
60 1
        $handler   = function ($method) use ($controllerClass) {
61 1
            return [$controllerClass, $method];
62 1
        };
63
64
        // if the class implements any of CRUD methods a corresponding route will be added
65 1
        $classInterfaces = class_implements($controllerClass);
66 1
        if (in_array(ControllerIndexInterface::class, $classInterfaces) === true) {
67 1
            $group->get($type, $handler(CI::METHOD_INDEX), $params(CI::METHOD_INDEX));
68
        }
69 1
        if (in_array(ControllerCreateInterface::class, $classInterfaces) === true) {
70 1
            $group->post($type, $handler(CI::METHOD_CREATE), $params(CI::METHOD_CREATE));
71
        }
72 1 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...
73 1
            $group->get($type . $indexSlug, $handler(CI::METHOD_READ), $params(CI::METHOD_READ));
74
        }
75 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...
76 1
            $group->patch($type . $indexSlug, $handler(CI::METHOD_UPDATE), $params(CI::METHOD_UPDATE));
77
        }
78 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...
79 1
            $group->delete($type . $indexSlug, $handler(CI::METHOD_DELETE), $params(CI::METHOD_DELETE));
80
        }
81
82 1
        return $group;
83
    }
84
85
    /**
86
     * @param GroupInterface $group
87
     * @param string         $subUri
88
     * @param string         $controllerClass
89
     *
90
     * @return GroupInterface
91
     */
92 1
    protected static function controller(GroupInterface $group, string $subUri, string $controllerClass): GroupInterface
93
    {
94 1
        $slugged = $subUri . '/{' . BaseController::ROUTE_KEY_INDEX . '}';
95 1
        $params  = function ($method) use ($subUri) {
96 1
            return [RouteInterface::PARAM_NAME => $subUri . '_' . $method];
97 1
        };
98 1
        $handler = function ($method) use ($controllerClass) {
99 1
            return [$controllerClass, $method];
100 1
        };
101
102
        // if the class implements any of CRUD methods a corresponding route will be added
103
        // as HTML forms do not support methods other than GET/POST we use POST and special URI for update and delete.
104 1
        $classInterfaces = class_implements($controllerClass);
105 1
        if (in_array(ControllerIndexInterface::class, $classInterfaces) === true) {
106 1
            $group->get($subUri, $handler(CI::METHOD_INDEX), $params(CI::METHOD_INDEX));
107
        }
108 1
        if (in_array(ControllerCreateInterface::class, $classInterfaces) === true) {
109 1
            $group->post($subUri, $handler(CI::METHOD_CREATE), $params(CI::METHOD_CREATE));
110
        }
111 1
        if (in_array(ControllerReadInterface::class, $classInterfaces) === true) {
112 1
            $group->get($slugged, $handler(CI::METHOD_READ), $params(CI::METHOD_READ));
113
        }
114 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...
115 1
            $updateUri = $slugged . '/' . CI::METHOD_UPDATE;
116 1
            $group->post($updateUri, $handler(CI::METHOD_UPDATE), $params(CI::METHOD_UPDATE));
117
        }
118 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...
119 1
            $deleteUri = $slugged . '/' . CI::METHOD_DELETE;
120 1
            $group->post($deleteUri, $handler(CI::METHOD_DELETE), $params(CI::METHOD_DELETE));
121
        }
122
123 1
        return $group;
124
    }
125
126
    /**
127
     * @param GroupInterface $group
128
     * @param string         $relationshipName
129
     * @param string         $controllerClass
130
     * @param string         $selfGetMethod
131
     *
132
     * @return GroupInterface
133
     */
134 1
    protected static function relationship(
135
        GroupInterface $group,
136
        string $relationshipName,
137
        string $controllerClass,
138
        string $selfGetMethod
139
    ): GroupInterface {
140
        /** @var BaseController $controllerClass */
141 1
        assert(array_key_exists(BaseController::class, class_parents($controllerClass)) === true);
142 1
        $schemaClass = $controllerClass::SCHEMA_CLASS;
143
144
        /** @var SchemaInterface $schemaClass */
145 1
        assert(array_key_exists(SchemaInterface::class, class_implements($schemaClass)) === true);
146 1
        $subUri = $schemaClass::TYPE;
147
148
        /** @var string $controllerClass */
149
        /** @var string $schemaClass */
150
151 1
        $resourceIdUri = $subUri . '/{' . CI::ROUTE_KEY_INDEX . '}/';
152 1
        $selfUri       = $resourceIdUri . DocumentInterface::KEYWORD_RELATIONSHIPS . '/' . $relationshipName;
153
154
        return $group
155
            // `self`
156 1
            ->get($selfUri, [$controllerClass, $selfGetMethod])
157
            // `related`
158 1
            ->get($resourceIdUri . $relationshipName, [$controllerClass, $selfGetMethod]);
159
    }
160
}
161