Passed
Push — master ( 3a6dc8...4b152c )
by Iman
04:34
created

AdminApiKeyController::getGenerateSecretKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ApiGeneratorModule;
4
5
use crocodicstudio\crudbooster\controllers\CBController;
6
use crocodicstudio\crudbooster\helpers\CbValidator;
7
use Illuminate\Support\Facades\DB;
8
9
class AdminApiKeyController extends CBController
10
{
11
    public function cbInit()
12
    {
13
        $this->table = 'cms_apicustom';
14
        $this->primaryKey = "id";
15
        $this->titleField = "nama";
16
        $this->button_show = false;
0 ignored issues
show
Bug Best Practice introduced by
The property button_show does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
        $this->button_new = false;
0 ignored issues
show
Bug Best Practice introduced by
The property button_new does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
        $this->deleteBtn = false;
19
        $this->buttonAdd = false;
20
        $this->button_import = false;
21
        $this->buttonExport = false;
22
    }
23
24
    public function getSecretKey()
25
    {
26
        $this->cbLoader();
27
        $data['page_title'] = 'API Generator';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
28
        $data['apikeys'] = DB::table('cms_apikey')->get();
29
30
        return view('CbApiGen::api_key', $data);
31
    }
32
33
    function getGenerateSecretKey()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
34
    {
35
        $this->cbLoader();
36
        //Generate a random string.
37
        $token = openssl_random_pseudo_bytes(16);
38
39
        //Convert the binary data into hexadecimal representation.
40
        $token = bin2hex($token);
41
42
        $id = DB::table('cms_apikey')->insertGetId([
43
                'secretkey' => $token,
44
                'created_at' => date('Y-m-d H:i:s'),
45
                'status' => 'active',
46
                'hit' => 0,
47
            ]);
48
49
        $response = [];
50
        $response['key'] = $token;
51
        $response['id'] = $id;
52
53
        return response()->json($response);
0 ignored issues
show
Bug introduced by
The method json() does not exist on Symfony\Component\HttpFoundation\Response. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\Response such as Illuminate\Http\Response or Illuminate\Http\JsonResponse or Illuminate\Http\RedirectResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        return response()->/** @scrutinizer ignore-call */ json($response);
Loading history...
54
    }
55
56
    public function getStatusApikey()
57
    {
58
        CbValidator::valid(['id' => 'required', 'status' => 'required'], 'view');
59
60
        $id = request('id');
61
        $status = (request('status') == 1) ? "active" : "non active";
62
63
        DB::table('cms_apikey')->where('id', $id)->update(['status' => $status]);
64
65
        backWithMsg('You have been update api key status !');
66
    }
67
68
    public function getDeleteApiKey()
69
    {
70
        $id = request('id');
71
        if (DB::table('cms_apikey')->where('id', $id)->delete()) {
72
            return response()->json(['status' => 1]);
73
        }
74
75
        return response()->json(['status' => 0]);
76
    }
77
78
}
79