Failed Conditions
Pull Request — master (#284)
by Maximo
02:41
created

UserWebhooksController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 83
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 2
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onConstruct() 0 12 1
B execute() 0 32 6
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;
11
use Canvas\Webhooks;
12
use Phalcon\Validation\Validator\PresenceOf;
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();
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 = [
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
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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