ClientController::update()   B
last analyzed

Complexity

Conditions 6
Paths 18

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 8.9617
c 0
b 0
f 0
ccs 0
cts 13
cp 0
cc 6
nc 18
nop 1
crap 42
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