Passed
Push — master ( 8227f6...25b390 )
by Iman
05:05
created

AdminApiKeyController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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
8
class AdminApiKeyController extends CBController
9
{
10
    private $apiKeysRepository;
11
12
    /**
13
     * AdminApiKeyController constructor.
14
     *
15
     * @param \crocodicstudio\crudbooster\Modules\ApiGeneratorModule\ApiKeysRepository $apiKeysRepository
16
     */
17
    public function __construct(ApiKeysRepository $apiKeysRepository)
18
    {
19
        $this->apiKeysRepository = $apiKeysRepository;
20
    }
21
22
    public function cbInit()
23
    {
24
        $this->table = 'cms_apicustom';
25
        $this->primaryKey = "id";
26
        $this->titleField = "name";
27
        $this->buttonShow = false;
28
        $this->deleteBtn = false;
29
        $this->buttonAdd = false;
30
        $this->buttonImport = false;
31
        $this->buttonExport = false;
32
    }
33
34
    public function getSecretKey()
35
    {
36
        $this->cbLoader();
37
        $data = [
38
            'page_title' => 'API Generator',
39
            'apikeys' => $this->apiKeysRepository->get(),
40
        ];
41
42
        return view('CbApiGen::api_key', $data);
43
    }
44
45
    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...
46
    {
47
        $this->cbLoader();
48
        //Generate a random string.
49
        $token = openssl_random_pseudo_bytes(16);
50
51
        //Convert the binary data into hexadecimal representation.
52
        $token = bin2hex($token);
53
        $id = $this->apiKeysRepository->insertGetId($token);
54
55
        $response = [
56
            'id' => $id,
57
            'key' => $token,
58
        ];
59
60
        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

60
        return response()->/** @scrutinizer ignore-call */ json($response);
Loading history...
61
    }
62
63
    public function getStatusApikey()
64
    {
65
        CbValidator::valid(['id' => 'required', 'status' => 'required'], 'view');
66
67
        $id = request('id');
68
        $status = (request('status') == 1) ? "active" : "non active";
69
70
        $this->apiKeysRepository->updateById($status, $id);
71
72
        backWithMsg('You have been update api key status !');
73
    }
74
75
    public function getDeleteApiKey()
76
    {
77
        if ($this->apiKeysRepository->deleteById(request('id'))) {
78
            return response()->json(['status' => 1]);
79
        }
80
81
        return response()->json(['status' => 0]);
82
    }
83
}
84