Skip to content

Commit 689e295

Browse files
committed
Use DB helper methods everywhere
1 parent b5f19ab commit 689e295

File tree

3 files changed

+41
-64
lines changed

3 files changed

+41
-64
lines changed

src/Plugin.php

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,6 @@ public function getCpNavItem()
285285
*/
286286
public function request(string $method, string $url, array $headers = null, string $body = null, int $webhookId = null, string $debounceKey = null)
287287
{
288-
$db = Craft::$app->getDb();
289-
290288
if ($debounceKey !== null) {
291289
// See if there is an existing pending request with the same key
292290
$requestId = (new Query())
@@ -314,22 +312,20 @@ public function request(string $method, string $url, array $headers = null, stri
314312
}
315313
}
316314

317-
$db->createCommand()
318-
->insert('{{%webhookrequests}}', [
319-
'webhookId' => $webhookId,
320-
'debounceKey' => $debounceKey,
321-
'status' => self::STATUS_PENDING,
322-
'method' => $method,
323-
'url' => $url,
324-
'requestHeaders' => $headers ? Json::encode($headers) : null,
325-
'requestBody' => $body,
326-
'dateCreated' => Db::prepareDateForDb(new DateTime()),
327-
'uid' => StringHelper::UUID(),
328-
], false)
329-
->execute();
315+
Db::insert('{{%webhookrequests}}', [
316+
'webhookId' => $webhookId,
317+
'debounceKey' => $debounceKey,
318+
'status' => self::STATUS_PENDING,
319+
'method' => $method,
320+
'url' => $url,
321+
'requestHeaders' => $headers ? Json::encode($headers) : null,
322+
'requestBody' => $body,
323+
'dateCreated' => Db::prepareDateForDb(new DateTime()),
324+
'uid' => StringHelper::UUID(),
325+
], false);
330326

331327
$this->_pendingJobs[] = new SendRequestJob([
332-
'requestId' => $db->getLastInsertID('{{%webhookrequests}}'),
328+
'requestId' => Craft::$app->getDb()->getLastInsertID('{{%webhookrequests}}'),
333329
'webhookId' => $webhookId,
334330
]);
335331

@@ -404,13 +400,10 @@ public function sendRequest(int $requestId): bool
404400
}
405401

406402
// Update the request
407-
$db = Craft::$app->getDb();
408-
$db->createCommand()
409-
->update('{{%webhookrequests}}', [
410-
'status' => self::STATUS_REQUESTED,
411-
'dateRequested' => Db::prepareDateForDb(new DateTime()),
412-
], ['id' => $requestId], [], false)
413-
->execute();
403+
Db::update('{{%webhookrequests}}', [
404+
'status' => self::STATUS_REQUESTED,
405+
'dateRequested' => Db::prepareDateForDb(new DateTime()),
406+
], ['id' => $requestId], [], false);
414407

415408
$startTime = microtime(true);
416409
$response = null;
@@ -431,17 +424,14 @@ public function sendRequest(int $requestId): bool
431424
$time = round(1000 * (microtime(true) - $startTime));
432425
$attempt = ($data['attempts'] ?? 0) + 1;
433426

434-
$db = Craft::$app->getDb();
435-
$db->createCommand()
436-
->update('{{%webhookrequests}}', [
437-
'status' => self::STATUS_DONE,
438-
'attempts' => $attempt,
439-
'responseStatus' => $response ? $response->getStatusCode() : null,
440-
'responseHeaders' => $response ? Json::encode($response->getHeaders()) : null,
441-
'responseBody' => $response ? (string)$response->getBody() : null,
442-
'responseTime' => $time,
443-
], ['id' => $requestId], [], false)
444-
->execute();
427+
Db::update('{{%webhookrequests}}', [
428+
'status' => self::STATUS_DONE,
429+
'attempts' => $attempt,
430+
'responseStatus' => $response ? $response->getStatusCode() : null,
431+
'responseHeaders' => $response ? Json::encode($response->getHeaders()) : null,
432+
'responseBody' => $response ? (string)$response->getBody() : null,
433+
'responseTime' => $time,
434+
], ['id' => $requestId], [], false);
445435

446436
// Release the lock
447437
$this->_unlockRequest($requestId);

src/WebhookManager.php

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Craft;
66
use craft\db\Query;
7+
use craft\helpers\Db;
78
use craft\helpers\Json;
89
use craft\helpers\StringHelper;
910
use yii\base\InvalidArgumentException;
@@ -69,19 +70,15 @@ public function saveGroup(Group $group, bool $runValidation = true): bool
6970
}
7071

7172
if ($group->id) {
72-
$db->createCommand()
73-
->update('{{%webhookgroups}}', [
74-
'name' => $name,
75-
], [
76-
'id' => $group->id,
77-
])
78-
->execute();
73+
Db::update('{{%webhookgroups}}', [
74+
'name' => $name,
75+
], [
76+
'id' => $group->id,
77+
]);
7978
} else {
80-
$db->createCommand()
81-
->insert('{{%webhookgroups}}', [
82-
'name' => $name,
83-
])
84-
->execute();
79+
Db::insert('{{%webhookgroups}}', [
80+
'name' => $name,
81+
]);
8582

8683
$group->id = (int)$db->getLastInsertID('{{%webhookgroups}}');
8784
}
@@ -96,11 +93,9 @@ public function saveGroup(Group $group, bool $runValidation = true): bool
9693
*/
9794
public function deleteGroupById(int $id)
9895
{
99-
Craft::$app->getDb()->createCommand()
100-
->delete('{{%webhookgroups}}', [
101-
'id' => $id,
102-
])
103-
->execute();
96+
Db::delete('{{%webhookgroups}}', [
97+
'id' => $id,
98+
]);
10499
}
105100

106101
// Webhooks
@@ -209,13 +204,9 @@ public function saveWebhook(Webhook $webhook, bool $runValidation = true): bool
209204
];
210205

211206
if ($webhook->id) {
212-
$db->createCommand()
213-
->update('{{%webhooks}}', $data, ['id' => $webhook->id])
214-
->execute();
207+
Db::update('{{%webhooks}}', $data, ['id' => $webhook->id]);
215208
} else {
216-
$db->createCommand()
217-
->insert('{{%webhooks}}', $data)
218-
->execute();
209+
Db::insert('{{%webhooks}}', $data);
219210
$webhook->id = (int)$db->getLastInsertID('{{%webhooks}}');
220211
}
221212

@@ -229,9 +220,7 @@ public function saveWebhook(Webhook $webhook, bool $runValidation = true): bool
229220
*/
230221
public function deleteWebhookById(int $id)
231222
{
232-
Craft::$app->getDb()->createCommand()
233-
->delete('{{%webhooks}}', ['id' => $id])
234-
->execute();
223+
Db::delete('{{%webhooks}}', ['id' => $id]);
235224
}
236225

237226
/**

src/controllers/ActivityController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Craft;
66
use craft\db\Paginator;
77
use craft\db\Query;
8+
use craft\helpers\Db;
89
use craft\helpers\Json;
910
use craft\web\twig\variables\Paginate;
1011
use craft\webhooks\assets\activity\ActivityAsset;
@@ -58,10 +59,7 @@ public function actionIndex(): Response
5859
*/
5960
public function actionClear(): Response
6061
{
61-
Craft::$app->getDb()->createCommand()
62-
->delete('{{%webhookrequests}}', ['status' => Plugin::STATUS_DONE])
63-
->execute();
64-
62+
Db::delete('{{%webhookrequests}}', ['status' => Plugin::STATUS_DONE]);
6563
return $this->redirect('webhooks/activity');
6664
}
6765

0 commit comments

Comments
 (0)