UserWebhooksController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 79
ccs 0
cts 12
cp 0
rs 10
c 1
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 29 6
A onConstruct() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Api\Controllers;
6
7
use Canvas\Http\Exception\UnprocessableEntityException;
8
use Canvas\Models\UserWebhooks;
9
use Canvas\Validation;
10
use Phalcon\Http\Response;
0 ignored issues
show
Bug introduced by
The type Phalcon\Http\Response 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
use Canvas\Webhooks;
12
use Phalcon\Validation\Validator\PresenceOf;
0 ignored issues
show
Bug introduced by
The type Phalcon\Validation\Validator\PresenceOf 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...
13
use function Canvas\Core\isJson;
14
15
/**
16
 * Class LanguagesController.
17
 *
18
 * @package Canvas\Api\Controllers
19
 *
20
 * @property Users $userData
21
 * @property Request $request
22
 * @property Config $config
23
 * @property Apps $app
24
 *
25
 */
26
class UserWebhooksController extends BaseController
27
{
28
    /*
29
     * fields we accept to create
30
     *
31
     * @var array
32
     */
33
    protected $createFields = [
34
        'webhooks_id',
35
        'url',
36
        'method',
37
        'format'
38
    ];
39
40
    /*
41
     * fields we accept to create
42
     *
43
     * @var array
44
     */
45
    protected $updateFields = [
46
        'webhooks_id',
47
        'url',
48
        'method',
49
        'format'
50
    ];
51
52
    /**
53
     * set objects.
54
     *
55
     * @return void
56
     */
57
    public function onConstruct()
58
    {
59
        $this->model = new UserWebhooks();
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...
60
        $this->model->users_id = $this->userData->getId();
61
        $this->model->companies_id = $this->userData->currentCompanyId();
62
        $this->model->apps_id = $this->app->getId();
63
        $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...
64
            ['is_deleted', ':', '0'],
65
            ['apps_id', ':', $this->app->getId()],
66
            ['companies_id', ':', '0|' . $this->userData->currentCompanyId()],
67
        ];
68
    }
69
70
    /**
71
    * Given the weebhook id, we run a test for it.
72
    *
73
    * @param integer $id
74
    * @return Response
75
    */
76
    public function execute(string $name): Response
77
    {
78
        $request = $this->request->getPostData();
79
        $validation = new Validation();
80
        $validateJson = false;
81
        $validation->add('module', new PresenceOf(['message' => 'module is required to know what webhook to run']));
82
        $validation->add('action', new PresenceOf(['message' => 'action is required']));
83
84
        if (isset($request['data']) && !empty($request['data'])) {
85
            if (!isJson($request['data'])) {
86
                throw new UnprocessableEntityException('Data is not a valid json format text');
87
            }
88
            $validation->add('data', new PresenceOf(['message' => 'data is required']));
89
            $validateJson = true;
90
        }
91
92
        $validation->validate($request);
93
94
        $systemModule = $request['module'];
95
        $data = $validateJson ? json_decode($request['data'], true) : [];
96
        $action = $request['action'];
97
        $headers = isJson((string) $request['headers']) ? json_decode($request['headers'], true) : [];
98
99
        return $this->response(
100
            Webhooks::process(
101
                $systemModule,
102
                $data,
103
                $action,
104
                $headers
105
            )
106
        );
107
    }
108
}
109