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 |
|
|
|
|
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 |
|
|
|
|
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()) { |
|
|
|
|
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
|
|
|
|
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 methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.