Completed
Push — main ( def8a0...e32e37 )
by ABRAHAM
01:26
created

KeyManager::response()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
namespace Bytesfield\KeyManager;
4
5
use Bytesfield\KeyManager\Models\ApiCredential;
6
use Bytesfield\KeyManager\Models\Client;
7
use Bytesfield\KeyManager\Traits\ApiResponse;
8
use Illuminate\Http\JsonResponse;
9
10
class KeyManager implements KeyManagerInterface
11
{
12
  use ApiResponse;
13
    /**
14
     * Create a new Client with Api credentials.
15
     *
16
     * @param string $name
17
     * @param string $type
18
     * @param string $status
19
     * @return \Illuminate\Http\JsonResponse
20
     */
21
    public function createClient(string $name, string $type, string $status = 'active'): JsonResponse
22
    {
23
        if (! in_array($status, [ApiCredential::STATUSES['ACTIVE'], ApiCredential::STATUSES['SUSPENDED']])) {
24
25
            return $this->error('Status must be either active or suspended');
26
        }
27
28
        $client = Client::create([
29
            'name' => $name,
30
            'type' => $type,
31
            'status' => $status,
32
        ]);
33
34
        $client->apiCredential()->create(ApiCredential::generateKeyPairArray());
35
36
        return $this->success('Client created successfully', $client->toArray());
37
    }
38
39
    /**
40
     * Get client's private key.
41
     *
42
     * @param int $client_id
43
     * @return \Illuminate\Http\JsonResponse
44
     */
45 View Code Duplication
    public function getPrivateKey(int $client_id): JsonResponse
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...
46
    {
47
        $client = Client::with('apiCredential')->where('id', $client_id)->first();
48
49
        if (! $client) {
50
51
            return $this->error("No client found with id $client_id");
52
        }
53
54
        return $this->success('Key retrieved successfully', ['key' => $client->apiCredential->private_key]);
55
    }
56
57
    /**
58
     * Change Client's public and private keys.
59
     *
60
     * @param int $client_id
61
     * @return \Illuminate\Http\JsonResponse
62
     */
63 View Code Duplication
    public function changeKeys(int $client_id): JsonResponse
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
        $key = ApiCredential::where('key_client_id', $client_id)->first();
66
67
        if (! $key) {
68
            return $this->error('Client information not found');
69
        }
70
71
        $key->update(ApiCredential::generateKeyPairArray());
72
73
        return $this->success('Api Keys successfully changed', ['key' => $key->private_key]);
74
    }
75
76
    /**
77
     * Suspend Client's Account.
78
     *
79
     * @param int $client_id
80
     * @return \Illuminate\Http\JsonResponse
81
     */
82
    public function suspendClient(int $client_id): JsonResponse
83
    {
84
        $client = Client::find($client_id);
85
86
        if (! $client) {
87
            return $this->error("No client found with id $client_id");
88
        }
89
90
        $client->update([
91
            'status' => ApiCredential::STATUSES['SUSPENDED'],
92
        ]);
93
94
        return $this->success('Client successfully suspended');
95
    }
96
97
    /**
98
     * Activate Client's Account.
99
     *
100
     * @param int $client_id
101
     * @return \Illuminate\Http\JsonResponse
102
     */
103 View Code Duplication
    public function activateClient(int $client_id): JsonResponse
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...
104
    {
105
        $client = Client::find($client_id);
106
107
        if (! $client) {
108
            return $this->error("No client found with id $client_id");
109
        }
110
111
        $client->update([
112
            'status' => ApiCredential::STATUSES['ACTIVE'],
113
        ]);
114
115
        return $this->success('Client successfully activated');
116
    }
117
118
    /**
119
     * Suspend Client's Api Credential.
120
     *
121
     * @param int $client_id
122
     * @return \Illuminate\Http\JsonResponse
123
     */
124 View Code Duplication
    public function suspendApiCredential(int $client_id): JsonResponse
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...
125
    {
126
        $key = ApiCredential::where('key_client_id', $client_id)->first();
127
128
        if (! $key) {
129
            return $this->error('Client information not found');
130
        }
131
132
        $key->update([
133
            'status' => ApiCredential::STATUSES['SUSPENDED'],
134
        ]);
135
136
        return $this->success('ApiCredential successfully suspended');
137
    }
138
139
    /**
140
     * Activate Client's Api Credential.
141
     *
142
     * @param int $client_id
143
     * @return \Illuminate\Http\JsonResponse
144
     */
145 View Code Duplication
    public function activateApiCredential(int $client_id): JsonResponse
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...
146
    {
147
        $key = ApiCredential::where('key_client_id', $client_id)->first();
148
149
        if (! $key) {
150
            return $this->error('Client information not found');
151
        }
152
153
        $key->update([
154
            'status' => ApiCredential::STATUSES['ACTIVE'],
155
        ]);
156
157
        return $this->success('ApiCredential successfully activated');
158
    }
159
160
}
161