Controller   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 129
Duplicated Lines 6.2 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 9
dl 8
loc 129
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A listAllUsers() 0 6 1
A listAllClients() 0 6 1
A listAllAdmins() 0 6 1
A refreshUser() 0 6 1
A getUser() 0 6 1
A registerUser() 0 7 1
A createAdmin() 0 6 1
A updateUser() 0 12 1
A deleteUser() 8 8 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\User\UI\API\Controllers;
4
5
use App\Containers\User\Actions\CreateAdminAction;
6
use App\Containers\User\Actions\CreateUserAction;
7
use App\Containers\User\Actions\DeleteUserAction;
8
use App\Containers\User\Actions\GetUserAction;
9
use App\Containers\User\Actions\ListAndSearchUsersAction;
10
use App\Containers\User\Actions\UpdateUserAction;
11
use App\Containers\User\UI\API\Requests\CreateAdminRequest;
12
use App\Containers\User\UI\API\Requests\DeleteUserRequest;
13
use App\Containers\User\UI\API\Requests\GetUserRequest;
14
use App\Containers\User\UI\API\Requests\ListAllUsersRequest;
15
use App\Containers\User\UI\API\Requests\RefreshUserRequest;
16
use App\Containers\User\UI\API\Requests\RegisterUserRequest;
17
use App\Containers\User\UI\API\Requests\UpdateUserRequest;
18
use App\Containers\User\UI\API\Transformers\UserTransformer;
19
use App\Ship\Parents\Controllers\ApiController;
20
21
/**
22
 * Class Controller.
23
 *
24
 * @author Mahmoud Zalt <[email protected]>
25
 */
26
class Controller extends ApiController
27
{
28
29
    /**
30
     * @param \App\Containers\User\UI\API\Requests\DeleteUserRequest $request
31
     * @param \App\Containers\User\Actions\DeleteUserAction          $action
32
     *
33
     * @return  \Dingo\Api\Http\Response
34
     */
35 View Code Duplication
    public function deleteUser(DeleteUserRequest $request, DeleteUserAction $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...
36
    {
37
        $user = $action->run($request->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Containers\Us...ests\DeleteUserRequest>. 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...
38
39
        return $this->response->accepted(null, [
40
            'message' => 'User (' . $user->getHashedKey() . ') Deleted Successfully.',
0 ignored issues
show
Bug introduced by
The method getHashedKey cannot be called on $user (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
41
        ]);
42
    }
43
44
    /**
45
     * @param \App\Containers\User\UI\API\Requests\ListAllUsersRequest $request
46
     * @param \App\Containers\User\Actions\ListAndSearchUsersAction    $action
47
     *
48
     * @return  \Dingo\Api\Http\Response
49
     */
50
    public function listAllUsers(ListAllUsersRequest $request, ListAndSearchUsersAction $action)
51
    {
52
        $users = $action->run();
53
54
        return $this->response->paginator($users, new UserTransformer());
55
    }
56
57
    /**
58
     * @param \App\Containers\User\UI\API\Requests\ListAllUsersRequest $request
59
     * @param \App\Containers\User\Actions\ListAndSearchUsersAction    $action
60
     *
61
     * @return  \Dingo\Api\Http\Response
62
     */
63
    public function listAllClients(ListAllUsersRequest $request, ListAndSearchUsersAction $action)
64
    {
65
        $users = $action->run(['client']);
66
67
        return $this->response->paginator($users, new UserTransformer());
68
    }
69
70
    /**
71
     * @param \App\Containers\User\UI\API\Requests\ListAllUsersRequest $request
72
     * @param \App\Containers\User\Actions\ListAndSearchUsersAction    $action
73
     *
74
     * @return  \Dingo\Api\Http\Response
75
     */
76
    public function listAllAdmins(ListAllUsersRequest $request, ListAndSearchUsersAction $action)
77
    {
78
        $users = $action->run(['admin']);
79
80
        return $this->response->paginator($users, new UserTransformer());
81
    }
82
83
    /**
84
     * @param \Dingo\Api\Http\Request                    $request
85
     * @param \App\Containers\User\Actions\GetUserAction $action
86
     *
87
     * @return  \Dingo\Api\Http\Response
88
     */
89
    public function refreshUser(RefreshUserRequest $request, GetUserAction $action)
90
    {
91
        $user = $action->run($request->id, $request->header('Authorization'));
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Containers\Us...sts\RefreshUserRequest>. 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...
92
93
        return $this->response->item($user, new UserTransformer());
94
    }
95
96
    /**
97
     * @param \App\Containers\User\UI\API\Requests\GetUserRequest $request
98
     * @param \App\Containers\User\Actions\GetUserAction          $action
99
     *
100
     * @return  \Dingo\Api\Http\Response
101
     */
102
    public function getUser(GetUserRequest $request, GetUserAction $action)
103
    {
104
        $user = $action->run($request->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Containers\Us...equests\GetUserRequest>. 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...
105
106
        return $this->response->item($user, new UserTransformer());
107
    }
108
109
    /**
110
     * @param \App\Containers\User\UI\API\Requests\RegisterUserRequest $request
111
     * @param \App\Containers\User\Actions\CreateUserAction            $action
112
     *
113
     * @return  \Dingo\Api\Http\Response
114
     */
115
    public function registerUser(RegisterUserRequest $request, CreateUserAction $action)
116
    {
117
        $user = $action->run($request['email'], $request['password'], $request['name'], $request['gender'],
118
            $request['birth'], true);
119
120
        return $this->response->item($user, new UserTransformer());
121
    }
122
123
    /**
124
     * @param \App\Containers\User\UI\API\Requests\CreateAdminRequest $request
125
     * @param \App\Containers\User\Actions\CreateAdminAction          $action
126
     *
127
     * @return  \Dingo\Api\Http\Response
128
     */
129
    public function createAdmin(CreateAdminRequest $request, CreateAdminAction $action)
130
    {
131
        $admin = $action->run($request['email'], $request['password'], $request['name']);
132
133
        return $this->response->item($admin, new UserTransformer());
134
    }
135
136
    /**
137
     * @param \App\Containers\User\UI\API\Requests\UpdateUserRequest $request
138
     * @param \App\Containers\User\Actions\UpdateUserAction          $action
139
     *
140
     * @return  \Dingo\Api\Http\Response
141
     */
142
    public function updateUser(UpdateUserRequest $request, UpdateUserAction $action)
143
    {
144
        $user = $action->run(
145
            $request['password'],
146
            $request['name'],
147
            $request['email'],
148
            $request['gender'],
149
            $request['birth']
150
        )->withToken();
151
152
        return $this->response->item($user, new UserTransformer());
153
    }
154
}
155