Completed
Push — develop ( 49cc42...1276fa )
by Neomerx
11:10 queued 08:22
created

FluteRoutesTrait::relationship()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 5
crap 1
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 Neomerx\JsonApi\Contracts\Document\DocumentInterface;
28
29
/**
30
 * @package Limoncello\Flute
31
 */
32
trait FluteRoutesTrait
33
{
34
    /**
35
     * @param GroupInterface $group
36
     * @param string         $resourceName
37
     * @param string         $controllerClass
38
     *
39
     * @return GroupInterface
40
     */
41 1
    protected static function resource(
42
        GroupInterface $group,
43
        string $resourceName,
44
        string $controllerClass
45
    ): GroupInterface {
46 1
        assert(class_exists($controllerClass) === true);
47
48 1
        $indexSlug = '/{' . CI::ROUTE_KEY_INDEX . '}';
49 1
        $params    = function (string $method) use ($resourceName): array {
50 1
            return [RouteInterface::PARAM_NAME => static::routeName($resourceName, $method)];
51 1
        };
52 1
        $handler   = function (string $method) use ($controllerClass): array {
53 1
            return [$controllerClass, $method];
54 1
        };
55
56
        // if the class implements any of CRUD methods a corresponding route will be added
57 1
        $classInterfaces = class_implements($controllerClass);
58 1 View Code Duplication
        if (in_array(ControllerIndexInterface::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...
59 1
            $group->get($resourceName, $handler(CI::METHOD_INDEX), $params(CI::METHOD_INDEX));
60
        }
61 1
        if (in_array(ControllerCreateInterface::class, $classInterfaces) === true) {
62 1
            $group->post($resourceName, $handler(CI::METHOD_CREATE), $params(CI::METHOD_CREATE));
63
        }
64 1
        if (in_array(ControllerReadInterface::class, $classInterfaces) === true) {
65 1
            $group->get($resourceName . $indexSlug, $handler(CI::METHOD_READ), $params(CI::METHOD_READ));
66
        }
67 1
        if (in_array(ControllerUpdateInterface::class, $classInterfaces) === true) {
68 1
            $group->patch($resourceName . $indexSlug, $handler(CI::METHOD_UPDATE), $params(CI::METHOD_UPDATE));
69
        }
70 1
        if (in_array(ControllerDeleteInterface::class, $classInterfaces) === true) {
71 1
            $group->delete($resourceName . $indexSlug, $handler(CI::METHOD_DELETE), $params(CI::METHOD_DELETE));
72
        }
73
74 1
        return $group;
75
    }
76
77
    /**
78
     * @param GroupInterface $group
79
     * @param string         $subUri
80
     * @param string         $controllerClass
81
     *
82
     * @return GroupInterface
83
     */
84 1
    protected static function controller(GroupInterface $group, string $subUri, string $controllerClass): GroupInterface
85
    {
86 1
        $slugged = $subUri . '/{' . CI::ROUTE_KEY_INDEX . '}';
87 1
        $params  = function (string $method) use ($subUri) : array {
88 1
            return [RouteInterface::PARAM_NAME => static::routeName($subUri, $method)];
89 1
        };
90 1
        $handler = function (string $method) use ($controllerClass): array {
91 1
            return [$controllerClass, $method];
92 1
        };
93
94
        // if the class implements any of CRUD methods a corresponding route will be added
95
        // as HTML forms do not support methods other than GET/POST we use POST and special URI for update and delete.
96 1
        $classInterfaces = class_implements($controllerClass);
97 1 View Code Duplication
        if (in_array(ControllerIndexInterface::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...
98 1
            $group->get($subUri, $handler(CI::METHOD_INDEX), $params(CI::METHOD_INDEX));
99
        }
100 1
        if (in_array(ControllerCreateInterface::class, $classInterfaces) === true) {
101 1
            $group->post($subUri, $handler(CI::METHOD_CREATE), $params(CI::METHOD_CREATE));
102
        }
103 1
        if (in_array(ControllerReadInterface::class, $classInterfaces) === true) {
104 1
            $group->get($slugged, $handler(CI::METHOD_READ), $params(CI::METHOD_READ));
105
        }
106 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...
107 1
            $updateUri = $slugged . '/' . CI::METHOD_UPDATE;
108 1
            $group->post($updateUri, $handler(CI::METHOD_UPDATE), $params(CI::METHOD_UPDATE));
109
        }
110 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...
111 1
            $deleteUri = $slugged . '/' . CI::METHOD_DELETE;
112 1
            $group->post($deleteUri, $handler(CI::METHOD_DELETE), $params(CI::METHOD_DELETE));
113
        }
114
115 1
        return $group;
116
    }
117
118
    /**
119
     * @param GroupInterface $group
120
     * @param string         $resourceName
121
     * @param string         $relationshipName
122
     * @param string         $controllerClass
123
     * @param string         $selfGetMethod
124
     *
125
     * @return GroupInterface
126
     */
127 1
    protected static function relationship(
128
        GroupInterface $group,
129
        string $resourceName,
130
        string $relationshipName,
131
        string $controllerClass,
132
        string $selfGetMethod
133
    ): GroupInterface {
134
        /** @var string $controllerClass */
135
        /** @var string $schemaClass */
136
137 1
        $resourceIdUri = $resourceName . '/{' . CI::ROUTE_KEY_INDEX . '}/';
138 1
        $selfUri       = $resourceIdUri . DocumentInterface::KEYWORD_RELATIONSHIPS . '/' . $relationshipName;
139
140
        return $group
141
            // `self`
142 1
            ->get($selfUri, [$controllerClass, $selfGetMethod])
143
            // `related`
144 1
            ->get($resourceIdUri . $relationshipName, [$controllerClass, $selfGetMethod]);
145
    }
146
147
    /**
148
     * @param string $name
149
     * @param string $method
150
     *
151
     * @return string
152
     */
153 2
    protected static function routeName(string $name, string $method): string
154
    {
155 2
        assert(empty($name) === false && empty($method) === false);
156
157 2
        return $name . '_' . $method;
158
    }
159
}
160