Failed Conditions
Pull Request — master (#284)
by Maximo
03:05
created

Webhooks::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 3
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas;
6
7
use Canvas\Models\UserWebhooks;
8
use Exception;
9
use Phalcon\Http\Response;
10
use GuzzleHttp\Client;
11
use Phalcon\Di;
12
use Canvas\Models\SystemModules;
13
14
/**
15
 * Class Validation.
16
 *
17
 * @package Canvas
18
 */
19
class Webhooks
20
{
21
    /**
22
    * Given the weebhook id, we run a test for it.
23
    *
24
    * @param integer $id
25
    * @param mixed $data
26
    * @return Response
27
    */
28
    public static function run(int $id, $data)
29
    {
30
        /**
31
         * 1- verify it s acorrect url
32
         * 2- verify the method
33
         * 3- get the entity info from one entity of this app and company
34
         * 4- guzzle the request with the info
35
         * 5- verify you got a 200
36
         * 6- return the response from the webhook.
37
         *
38
         * later - add job for all system module to execute a queue when CRUD acction are run, maybe the middleware would do this?
39
         *
40
         */
41
        $userWebhook = UserWebhooks::getById($id);
42
43
        $client = new Client();
44
        $parse = function ($error) {
45
            if ($error->hasResponse()) {
46
                return $error->getResponse();
47
            }
48
            return json_decode($error->getMessage());
49
        };
50
51
        try {
52
            $clientRequest = $client->request(
53
                $userWebhook->method,
54
                $userWebhook->url,
55
                $data
56
            );
57
58
            $response = $clientRequest->getBody();
59
        } catch (Exception $error) {
60
            $response = $parse($error);
61
        }
62
63
        return $response;
64
    }
65
66
    /**
67
     * Execute the the webhook for the given app company providing the system module
68
     * for the SDK
69
     *  - pass the system module classname or classname\namespace
70
     *  - pass the action you are doing from the CRUD
71
     *  - get all the hooks from the user that match this action
72
     *  - pass the data you are sending
73
     *  - then we send it over to the URl.
74
     *
75
     * @param string $model
0 ignored issues
show
Bug introduced by
There is no parameter named $model. 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...
76
     * @param mixed $data
77
     * @param string $action
78
     * @throws Exception
79
     * @return bool
80
     */
81
    public static function process(string $module, $data, string $action): bool
0 ignored issues
show
Coding Style introduced by
function process() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
82
    {
83
        $appId = Di::getDefault()->getApp()->getId();
84
        $company = Di::getDefault()->getUserData()->getDefaultCompany();
85
86
        $systemModule = SystemModules::getByName($module);
87
88
        $webhooks = UserWebhooks::find([
89
            'conditions' => 'apps_id = ?0 AND companies_id = ?1 
90
                            AND webhooks_id in 
91
                                (SELECT id FROM Canvas\Models\Webhooks WHERE apps_id = ?0 AND system_modules_id = ?2 AND action = ?3)',
92
            'bind' => [
93
                $appId,
94
                $company->getId(),
95
                $systemModule->getId(),
96
                $action
97
            ]
98
        ]);
99
100
        if ($webhook->count()) {
0 ignored issues
show
Bug introduced by
The variable $webhook seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
101
            foreach ($webhooks as $webhook) {
102
                self::run($webhook->getId(), $data);
103
            }
104
105
            return true;
106
        }
107
108
        return false;
109
    }
110
}
111
112
Webhooks::process('Companies', $this->toArray(), 'create');
113