ClientController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 99
Duplicated Lines 42.42 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 42
loc 99
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 10 1
B store() 21 21 6
B update() 21 21 6
A destroy() 0 13 2

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 CodexShaper\OAuth2\Server\Http\Controllers;
4
5
use CodexShaper\OAuth2\Server\Model;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Str;
8
9
class ClientController
10
{
11
    /**
12
     * Get all clients.
13
     *
14
     * @param \Illuminate\Http\Request $request
15
     *
16
     * @return json
17
     */
18
    public function all(Request $request)
19
    {
20
        $clients = Model::instance('clientModel')
21
                   ->whereUserId($request->user_id)
22
                   ->orderBy('updated_at', 'DESC')
23
                   ->get()
24
                   ->makeVisible('secret');
25
26
        return json_encode(['clients' => $clients]);
27
    }
28
29
    /**
30
     * Create a client.
31
     *
32
     * @param \Illuminate\Http\Request $request
33
     *
34
     * @return json
35
     */
36 View Code Duplication
    public function store(Request $request)
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...
37
    {
38
        try {
39
            $client = Model::instance('clientModel');
40
41
            $client->user_id = $request->user_id;
42
            $client->name = $request->name;
43
            $client->redirect = !empty($request->redirect) ? $request->redirect : 'http://localhost';
44
            $client->secret = Str::random(40);
45
            $client->personal_access_client = $request->type == 'personal_access' ? 1 : 0;
46
            $client->password_client = $request->type == 'password' ? 1 : 0;
47
            $client->authorization_code_client = $request->type == 'authorization_code' ? 1 : 0;
48
            $client->revoked = 0;
49
50
            $client->save();
51
        } catch (\Exception $ex) {
52
            return json_encode(['errors' => [$ex->getMessage()]], 404);
53
        }
54
55
        return $this->all($request);
56
    }
57
58
    /**
59
     * Modify a client.
60
     *
61
     * @param \Illuminate\Http\Request $request
62
     *
63
     * @return json
64
     */
65 View Code Duplication
    public function update(Request $request)
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...
66
    {
67
        try {
68
            $client = Model::instance('clientModel')->find($request->id);
69
70
            $client->user_id = $request->user_id;
71
            $client->name = $request->name;
72
            $client->redirect = !empty($request->redirect) ? $request->redirect : 'http://localhost';
73
            // $client->secret = Str::random(40);
74
            $client->personal_access_client = $request->type == 'personal_access' ? 1 : 0;
75
            $client->password_client = $request->type == 'password' ? 1 : 0;
76
            $client->authorization_code_client = $request->type == 'authorization_code' ? 1 : 0;
77
            $client->revoked = 0;
78
79
            $client->save();
80
        } catch (\Exception $ex) {
81
            return json_encode(['errors' => [$ex->getMessage()]], 404);
82
        }
83
84
        return $this->all($request);
85
    }
86
87
    /**
88
     * Destroy a client.
89
     *
90
     * @param \Illuminate\Http\Request $request
91
     *
92
     * @return json
93
     */
94
    public function destroy(Request $request)
95
    {
96
        $client = Model::instance('clientModel')->whereId($request->id)->whereUserId($request->user_id)->first();
97
98
        if ($client) {
99
            $client->tokens()->delete();
100
            $client->delete();
101
102
            return $this->all($request);
103
        }
104
105
        return json_encode(['errors' => ['There is no client.']], 404);
106
    }
107
}
108