Completed
Push — master ( 014612...36ad26 )
by Mahmoud
03:39
created

Controller   B

Complexity

Total Complexity 12

Size/Duplication

Total Lines 169
Duplicated Lines 4.73 %

Coupling/Cohesion

Components 0
Dependencies 16

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 0
cbo 16
dl 8
loc 169
rs 8.4614

12 Methods

Rating   Name   Duplication   Size   Complexity  
A listAllPermissions() 0 6 1
A getPermission() 0 6 1
A listAllRoles() 0 6 1
A getRole() 0 6 1
A assignUserToRole() 0 6 1
A deleteRole() 8 8 1
A revokeRoleFromUser() 0 6 1
A attachPermissionToRole() 0 8 1
A syncPermissionOnRole() 0 8 1
A detachPermissionFromRole() 0 8 1
A createRole() 0 6 1
A createPermission() 0 6 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
2
3
namespace App\Containers\Authorization\UI\API\Controllers;
4
5
use App\Containers\Authorization\Actions\AssignUserToRoleAction;
6
use App\Containers\Authorization\Actions\AttachPermissionsToRoleAction;
7
use App\Containers\Authorization\Actions\CreatePermissionAction;
8
use App\Containers\Authorization\Actions\CreateRoleAction;
9
use App\Containers\Authorization\Actions\DeleteRoleAction;
10
use App\Containers\Authorization\Actions\DetachPermissionsFromRoleAction;
11
use App\Containers\Authorization\Actions\GetPermissionAction;
12
use App\Containers\Authorization\Actions\GetRoleAction;
13
use App\Containers\Authorization\Actions\ListAllPermissionsAction;
14
use App\Containers\Authorization\Actions\ListAllRolesAction;
15
use App\Containers\Authorization\Actions\RevokeUserFromRoleAction;
16
use App\Containers\Authorization\Actions\SyncPermissionsOnRoleAction;
17
use App\Containers\Authorization\UI\API\Requests\AssignUserToRoleRequest;
18
use App\Containers\Authorization\UI\API\Requests\AttachPermissionToRoleRequest;
19
use App\Containers\Authorization\UI\API\Requests\CreatePermissionRequest;
20
use App\Containers\Authorization\UI\API\Requests\CreateRoleRequest;
21
use App\Containers\Authorization\UI\API\Requests\DeleteRoleRequest;
22
use App\Containers\Authorization\UI\API\Requests\DetachPermissionToRoleRequest;
23
use App\Containers\Authorization\UI\API\Requests\GetPermissionRequest;
24
use App\Containers\Authorization\UI\API\Requests\GetRoleRequest;
25
use App\Containers\Authorization\UI\API\Requests\ListAllPermissionsRequest;
26
use App\Containers\Authorization\UI\API\Requests\ListAllRolesRequest;
27
use App\Containers\Authorization\UI\API\Requests\RevokeUserFromRoleRequest;
28
use App\Containers\Authorization\UI\API\Requests\SyncPermissionOnRoleRequest;
29
use App\Containers\Authorization\UI\API\Transformers\PermissionTransformer;
30
use App\Containers\Authorization\UI\API\Transformers\RoleTransformer;
31
use App\Containers\User\Models\User;
32
use App\Containers\User\UI\API\Transformers\UserTransformer;
33
use App\Ship\Parents\Controllers\ApiController;
34
35
/**
36
 * Class Controller.
37
 *
38
 * @author Mahmoud Zalt <[email protected]>
39
 */
40
class Controller extends ApiController
41
{
42
43
    /**
44
     * @param \App\Containers\Authorization\UI\API\Requests\ListAllPermissionsRequest $request
45
     * @param \App\Containers\Authorization\Actions\ListAllPermissionsAction          $action
46
     *
47
     * @return  \Dingo\Api\Http\Response
48
     */
49
    public function listAllPermissions(ListAllPermissionsRequest $request, ListAllPermissionsAction $action)
50
    {
51
        $permissions = $action->run();
52
53
        return $this->response->collection($permissions, new PermissionTransformer());
54
    }
55
56
    /**
57
     * @param \App\Containers\Authorization\UI\API\Requests\GetPermissionRequest $request
58
     * @param \App\Containers\Authorization\Actions\GetPermissionAction          $action
59
     *
60
     * @return  \Dingo\Api\Http\Response
61
     */
62
    public function getPermission(GetPermissionRequest $request, GetPermissionAction $action)
63
    {
64
        $permission = $action->run($request['id']);
65
66
        return $this->response->item($permission, new PermissionTransformer());
67
    }
68
69
    /**
70
     * @param \App\Containers\Authorization\UI\API\Requests\ListAllRolesRequest $request
71
     * @param \App\Containers\Authorization\Actions\ListAllRolesAction          $action
72
     *
73
     * @return  \Dingo\Api\Http\Response
74
     */
75
    public function listAllRoles(ListAllRolesRequest $request, ListAllRolesAction $action)
76
    {
77
        $roles = $action->run();
78
79
        return $this->response->collection($roles, new RoleTransformer());
80
    }
81
82
    /**
83
     * @param \App\Containers\Authorization\UI\API\Requests\GetRoleRequest $request
84
     * @param \App\Containers\Authorization\Actions\GetRoleAction          $action
85
     *
86
     * @return  \Dingo\Api\Http\Response
87
     */
88
    public function getRole(GetRoleRequest $request, GetRoleAction $action)
89
    {
90
        $role = $action->run($request['id']);
91
92
        return $this->response->item($role, new RoleTransformer());
93
    }
94
95
    /**
96
     * @param \App\Containers\Authorization\UI\API\Requests\AssignUserToRoleRequest $request
97
     * @param \App\Containers\Authorization\Actions\AssignUserToRoleAction                $action
98
     *
99
     * @return  \Dingo\Api\Http\Response
100
     */
101
    public function assignUserToRole(AssignUserToRoleRequest $request, AssignUserToRoleAction $action)
102
    {
103
        $user = $action->run($request['user_id'], $request['roles_ids']);
104
105
        return $this->response->item($user, new UserTransformer());
106
    }
107
108
    /**
109
     * @param \App\Containers\Authorization\UI\API\Requests\DeleteRoleRequest $request
110
     * @param \App\Containers\Authorization\Actions\DeleteRoleAction          $action
111
     *
112
     * @return  \Dingo\Api\Http\Response
113
     */
114 View Code Duplication
    public function deleteRole(DeleteRoleRequest $request, DeleteRoleAction $action)
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...
115
    {
116
        $action->run($request->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Containers\Au...ests\DeleteRoleRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117
118
        return $this->response->accepted(null, [
119
            'message' => 'Role (' . $request->id . ') Deleted Successfully.',
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Containers\Au...ests\DeleteRoleRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
120
        ]);
121
    }
122
123
    /**
124
     * @param \App\Containers\Authorization\UI\API\Requests\RevokeUserFromRoleRequest $request
125
     * @param \App\Containers\Authorization\Actions\RevokeUserFromRoleAction          $action
126
     *
127
     * @return  \Dingo\Api\Http\Response
128
     */
129
    public function revokeRoleFromUser(RevokeUserFromRoleRequest $request, RevokeUserFromRoleAction $action)
130
    {
131
        $user = $action->run($request['user_id'], $request['roles_ids']);
132
133
        return $this->response->item($user, new UserTransformer());
134
    }
135
136
    /**
137
     * @param \App\Containers\Authorization\UI\API\Requests\AttachPermissionToRoleRequest $request
138
     * @param \App\Containers\Authorization\Actions\AttachPermissionsToRoleAction         $action
139
     *
140
     * @return  \Dingo\Api\Http\Response
141
     */
142
    public function attachPermissionToRole(
143
        AttachPermissionToRoleRequest $request,
144
        AttachPermissionsToRoleAction $action
145
    ) {
146
        $role = $action->run($request['role_id'], $request['permissions_ids']);
147
148
        return $this->response->item($role, new RoleTransformer());
149
    }
150
151
    /**
152
     * @param \App\Containers\Authorization\UI\API\Requests\SyncPermissionOnRoleRequest $request
153
     * @param \App\Containers\Authorization\Actions\SyncPermissionsOnRoleAction         $action
154
     *
155
     * @return  \Dingo\Api\Http\Response
156
     */
157
    public function syncPermissionOnRole(
158
        SyncPermissionOnRoleRequest $request,
159
        SyncPermissionsOnRoleAction $action
160
    ) {
161
        $role = $action->run($request['role_id'], $request['permissions_ids']);
162
163
        return $this->response->item($role, new RoleTransformer());
164
    }
165
166
    /**
167
     * @param \App\Containers\Authorization\UI\API\Requests\DetachPermissionToRoleRequest $request
168
     * @param \App\Containers\Authorization\Actions\DetachPermissionsFromRoleAction       $action
169
     *
170
     * @return  \Dingo\Api\Http\Response
171
     */
172
    public function detachPermissionFromRole(
173
        DetachPermissionToRoleRequest $request,
174
        DetachPermissionsFromRoleAction $action
175
    ) {
176
        $role = $action->run($request['role_id'], $request['permissions_ids']);
177
178
        return $this->response->item($role, new RoleTransformer());
179
    }
180
181
    /**
182
     * @param \App\Containers\Authorization\UI\API\Requests\CreateRoleRequest $request
183
     * @param \App\Containers\Authorization\Actions\CreateRoleAction          $action
184
     *
185
     * @return  \Dingo\Api\Http\Response
186
     */
187
    public function createRole(CreateRoleRequest $request, CreateRoleAction $action)
188
    {
189
        $role = $action->run($request['name'], $request['description'], $request['display_name']);
190
191
        return $this->response->item($role, new RoleTransformer());
192
    }
193
194
    /**
195
     * @param \App\Containers\Authorization\UI\API\Requests\CreatePermissionRequest $request
196
     * @param \App\Containers\Authorization\Actions\CreatePermissionAction          $action
197
     *
198
     * @return  \Dingo\Api\Http\Response
199
     */
200
    public function createPermission(CreatePermissionRequest $request, CreatePermissionAction $action)
201
    {
202
        $permission = $action->run($request['name'], $request['description'], $request['display_name']);
203
204
        return $this->response->item($permission, new PermissionTransformer());
205
    }
206
207
208
}
209