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
|
|
|
/** |
15
|
|
|
* Create a new Client with Api credentials. |
16
|
|
|
* |
17
|
|
|
* @param string $name |
18
|
|
|
* @param string $type |
19
|
|
|
* @param string $status |
20
|
|
|
* @return \Illuminate\Http\JsonResponse |
21
|
|
|
*/ |
22
|
|
|
public function createClient(string $name, string $type, string $status = 'active'): JsonResponse |
23
|
|
|
{ |
24
|
|
|
if (! in_array($status, [ApiCredential::STATUSES['ACTIVE'], ApiCredential::STATUSES['SUSPENDED']])) { |
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 |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$client = Client::with('apiCredential')->where('id', $client_id)->first(); |
48
|
|
|
|
49
|
|
|
if (! $client) { |
50
|
|
|
return $this->error("No client found with id $client_id"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->success('Key retrieved successfully', ['key' => $client->apiCredential->private_key]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Change Client's public and private keys. |
58
|
|
|
* |
59
|
|
|
* @param int $client_id |
60
|
|
|
* @return \Illuminate\Http\JsonResponse |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function changeKeys(int $client_id): JsonResponse |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$key = ApiCredential::where('key_client_id', $client_id)->first(); |
65
|
|
|
|
66
|
|
|
if (! $key) { |
67
|
|
|
return $this->error('Client information not found'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$key->update(ApiCredential::generateKeyPairArray()); |
71
|
|
|
|
72
|
|
|
return $this->success('Api Keys successfully changed', ['key' => $key->private_key]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Suspend Client's Account. |
77
|
|
|
* |
78
|
|
|
* @param int $client_id |
79
|
|
|
* @return \Illuminate\Http\JsonResponse |
80
|
|
|
*/ |
81
|
|
|
public function suspendClient(int $client_id): JsonResponse |
82
|
|
|
{ |
83
|
|
|
$client = Client::find($client_id); |
84
|
|
|
|
85
|
|
|
if (! $client) { |
86
|
|
|
return $this->error("No client found with id $client_id"); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$client->update([ |
90
|
|
|
'status' => ApiCredential::STATUSES['SUSPENDED'], |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
return $this->success('Client successfully suspended'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Activate Client's Account. |
98
|
|
|
* |
99
|
|
|
* @param int $client_id |
100
|
|
|
* @return \Illuminate\Http\JsonResponse |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function activateClient(int $client_id): JsonResponse |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$client = Client::find($client_id); |
105
|
|
|
|
106
|
|
|
if (! $client) { |
107
|
|
|
return $this->error("No client found with id $client_id"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$client->update([ |
111
|
|
|
'status' => ApiCredential::STATUSES['ACTIVE'], |
112
|
|
|
]); |
113
|
|
|
|
114
|
|
|
return $this->success('Client successfully activated'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Suspend Client's Api Credential. |
119
|
|
|
* |
120
|
|
|
* @param int $client_id |
121
|
|
|
* @return \Illuminate\Http\JsonResponse |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function suspendApiCredential(int $client_id): JsonResponse |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$key = ApiCredential::where('key_client_id', $client_id)->first(); |
126
|
|
|
|
127
|
|
|
if (! $key) { |
128
|
|
|
return $this->error('Client information not found'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$key->update([ |
132
|
|
|
'status' => ApiCredential::STATUSES['SUSPENDED'], |
133
|
|
|
]); |
134
|
|
|
|
135
|
|
|
return $this->success('ApiCredential successfully suspended'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Activate Client's Api Credential. |
140
|
|
|
* |
141
|
|
|
* @param int $client_id |
142
|
|
|
* @return \Illuminate\Http\JsonResponse |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
public function activateApiCredential(int $client_id): JsonResponse |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$key = ApiCredential::where('key_client_id', $client_id)->first(); |
147
|
|
|
|
148
|
|
|
if (! $key) { |
149
|
|
|
return $this->error('Client information not found'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$key->update([ |
153
|
|
|
'status' => ApiCredential::STATUSES['ACTIVE'], |
154
|
|
|
]); |
155
|
|
|
|
156
|
|
|
return $this->success('ApiCredential successfully activated'); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
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.