Failed Conditions
Push — master ( 05d004...385b2f )
by Maximo
19s queued 13s
created

AppsKeysController::processCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Api\Controllers;
6
7
use Canvas\Http\Response;
8
use Canvas\Models\AppsKeys;
9
use Phalcon\Http\RequestInterface;
10
use Phalcon\Mvc\ModelInterface;
0 ignored issues
show
Bug introduced by
The type Phalcon\Mvc\ModelInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * Class CompaniesController.
14
 *
15
 * @package Canvas\Api\Controllers
16
 *
17
 * @property Users $userData
18
 * @property Request $request
19
 */
20
class AppsKeysController extends BaseController
21
{
22
    /*
23
     * fields we accept to create
24
     *
25
     * @var array
26
     */
27
    protected $createFields = [
28
        'client_id',
29
        'client_secret_id',
30
        'apps_id',
31
        'users_id',
32
    ];
33
34
    /*
35
     * fields we accept to create
36
     *
37
     * @var array
38
     */
39
    protected $updateFields = [
40
        'last_used_date',
41
    ];
42
43
    /**
44
     * set objects.
45
     *
46
     * @return void
47
     */
48
    public function onConstruct()
49
    {
50
        $this->model = new AppsKeys();
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
51
        $this->model->users_id = $this->userData->getId();
52
        $this->model->apps_id = $this->app->getId();
53
54
        $this->additionalSearchFields = [
0 ignored issues
show
Bug Best Practice introduced by
The property additionalSearchFields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
55
            ['apps_id', ':', $this->app->getId()],
56
        ];
57
    }
58
59
    /**
60
     * Process the create request and trecurd the boject.
61
     *
62
     * @return ModelInterface
63
     * @throws Exception
64
     */
65
    protected function processCreate(RequestInterface $request): ModelInterface
66
    {
67
        $this->model->client_id = bin2hex(random_bytes(64));
68
        $this->model->client_secret_id = bin2hex(random_bytes(64));
69
        $this->model->saveOrFail();
70
71
        return $this->model;
72
    }
73
74
    /**
75
     * Regenerate both client id and client secret id.
76
     *
77
     * @return Response
78
     */
79
    public function regenerateKeys() : Response
80
    {
81
        $appsKeys = AppsKeys::findFirstOrFail([
82
            'conditions' => 'users_id = ?0 and apps_id = ?1 and is_deleted = 0',
83
            'bind' => [$this->userData->getId(), $this->app->getId()]
84
        ]);
85
86
        $appsKeys->client_id = bin2hex(random_bytes(64));
87
        $appsKeys->client_secret_id = bin2hex(random_bytes(64));
88
        $appsKeys->saveOrFail();
89
90
        return $this->response($appsKeys);
91
    }
92
}
93