Skip to content

Commit

Permalink
Convert webhook GraphQL responses to ResponseAccess obj
Browse files Browse the repository at this point in the history
  • Loading branch information
Onur Kose committed Jun 20, 2021
1 parent e1b69e1 commit 790fd23
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
17 changes: 9 additions & 8 deletions src/ShopifyApp/Actions/CreateWebhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Osiset\ShopifyApp\Actions;

use Osiset\BasicShopifyAPI\ResponseAccess;
use Osiset\ShopifyApp\Contracts\Objects\Values\ShopId as ShopIdValue;
use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;

Expand Down Expand Up @@ -44,13 +45,13 @@ public function __invoke(ShopIdValue $shopId, array $configWebhooks): array
* Checks if a webhooks exists already in the shop.
*
* @param array $webhook The webhook config.
* @param array $webhooks The current webhooks to search.
* @param ResponseAccess $webhooks The current webhooks to search.
*
* @return bool
*/
$exists = static function (array $webhook, array $webhooks): bool {
foreach ($webhooks as $shopWebhook) {
if ($shopWebhook['node']['endpoint']['callbackUrl'] === $webhook['address']) {
$exists = static function (array $webhook, ResponseAccess $webhooks): bool {
foreach (data_get($webhooks, 'data.webhookSubscriptions.container.edges', []) as $shopWebhook) {
if (data_get($shopWebhook, 'node.endpoint.callbackUrl') === $webhook['address']) {
// Found the webhook in our list
return true;
}
Expand All @@ -71,7 +72,7 @@ public function __invoke(ShopIdValue $shopId, array $configWebhooks): array
$used = [];
foreach ($configWebhooks as $webhook) {
// Check if the required webhook exists on the shop
if (! $exists($webhook, $webhooks['container']['edges'])) {
if (! $exists($webhook, $webhooks)) {
// It does not... create the webhook
$apiHelper->createWebhook($webhook);
$created[] = $webhook;
Expand All @@ -81,10 +82,10 @@ public function __invoke(ShopIdValue $shopId, array $configWebhooks): array
}

// Delete unused webhooks
foreach ($webhooks['container']['edges'] as $webhook) {
if (! in_array($webhook['node']['endpoint']['callbackUrl'], $used)) {
foreach (data_get($webhooks, 'data.webhookSubscriptions.container.edges', []) as $webhook) {
if (! in_array(data_get($webhook, 'node.endpoint.callbackUrl'), $used)) {
// Webhook should be deleted
$apiHelper->deleteWebhook($webhook['node']['id']);
$apiHelper->deleteWebhook(data_get($webhook, 'node.id'));
$deleted[] = $webhook;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ShopifyApp/Actions/DeleteWebhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public function __invoke(ShopId $shopId): array
$webhooks = $apiHelper->getWebhooks();

$deleted = [];
foreach ($webhooks['container']['edges'] as $webhook) {
foreach (data_get($webhooks, 'data.webhookSubscriptions.container.edges', []) as $webhook) {
// Its a webhook in the config, delete it
$apiHelper->deleteWebhook($webhook['node']['id']);
$apiHelper->deleteWebhook(data_get($webhook, 'node.id'));

// Keep track of what was deleted
$deleted[] = $webhook;
Expand Down
6 changes: 3 additions & 3 deletions src/ShopifyApp/Services/ApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public function getWebhooks(array $params = []): ResponseAccess

$response = $this->doRequestGraphQL($query, $variables);

return $response['body']['data']['webhookSubscriptions'];
return $response['body'];
}

/**
Expand Down Expand Up @@ -393,7 +393,7 @@ public function createWebhook(array $payload): ResponseAccess

$response = $this->doRequestGraphQL($query, $variables);

return $response['body']['data']['webhookSubscriptionCreate'];
return $response['body'];
}

/**
Expand All @@ -420,7 +420,7 @@ public function deleteWebhook(string $webhookId): ResponseAccess

$response = $this->doRequestGraphQL($query, $variables);

return $response['body']['data']['webhookSubscriptionDelete'];
return $response['body'];
}

/**
Expand Down

0 comments on commit 790fd23

Please sign in to comment.