ClientController::all()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace CodexShaper\OAuth2\Server\Http\Controllers;
4
5
use CodexShaper\OAuth2\Server\Manager;
6
use CodexShaper\OAuth2\Server\Model;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Controller;
9
use Illuminate\Support\Str;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
13
class ClientController extends Controller
14
{
15
    public function issueAccessToken(ServerRequestInterface $request, ResponseInterface $response)
16
    {
17
        $manager = new Manager();
18
        $server = $manager->makeAuthorizationServer();
19
20
        try {
21
            $response = $server->respondToAccessTokenRequest($request, $response);
22
23
            return wp_send_json(json_decode($response->getBody()), 200);
24
        } catch (\Exception $e) {
25
            return wp_send_json(['errors' => [$ex->getMessage()]], 404);
0 ignored issues
show
Bug introduced by
The variable $ex does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
26
        }
27
    }
28
29
    public function all(Request $request)
30
    {
31
        $clients = Model::instance('clientModel')
32
                   ->whereUserId($request->user()->getKey())
33
                   ->orderBy('updated_at', 'DESC')
34
                   ->get()
35
                   ->makeVisible('secret');
36
37
        return wp_send_json(['clients' => $clients]);
38
    }
39
40 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...
41
    {
42
        try {
43
            $client = Model::instance('clientModel');
44
45
            $client->user_id = $request->user()->getKey();
46
            $client->name = $request->name;
47
            $client->secret = Str::random(40);
48
            $client->redirect = !empty($request->redirect) ? $request->redirect : 'http://localhost';
49
            $client->scopes = explode(',', $request->scopes);
50
            $client->personal_access_client = $request->type == 'personal_access' ? 1 : 0;
51
            $client->password_client = $request->type == 'password' ? 1 : 0;
52
            $client->authorization_code_client = $request->type == 'authorization_code' ? 1 : 0;
53
            $client->revoked = 0;
54
55
            $client->save();
56
        } catch (\Exception $ex) {
57
            return wp_send_json(['errors' => [$ex->getMessage()]], 404);
58
        }
59
60
        return $this->all($request);
61
    }
62
63 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...
64
    {
65
        try {
66
            $client = Model::instance('clientModel')->find($request->id);
67
68
            $client->user_id = $request->user()->getKey();
69
            $client->name = $request->name;
70
            $client->redirect = !empty($request->redirect) ? $request->redirect : 'http://localhost';
71
            // $client->secret = Str::random(40);
72
            $client->scopes = explode(',', $request->scopes);
73
            $client->personal_access_client = $request->type == 'personal_access' ? 1 : 0;
74
            $client->password_client = $request->type == 'password' ? 1 : 0;
75
            $client->authorization_code_client = $request->type == 'authorization_code' ? 1 : 0;
76
            $client->revoked = 0;
77
78
            $client->save();
79
        } catch (\Exception $ex) {
80
            return wp_send_json(['errors' => [$ex->getMessage()]], 404);
81
        }
82
83
        return $this->all($request);
84
    }
85
86
    public function destroy(Request $request)
87
    {
88
        $client = Model::instance('clientModel')->whereId($request->id)->whereUserId($request->user()->getKey())->first();
89
90
        if ($client) {
91
            $client->tokens()->delete();
92
            $client->delete();
93
94
            return $this->all($request);
95
        }
96
97
        return wp_send_json(['errors' => ['There is no client.']], 404);
98
    }
99
}
100