|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OhMyBrew\ShopifyApp\Services; |
| 4 | + |
| 5 | +use Illuminate\Support\Collection; |
| 6 | +use Illuminate\Support\Facades\Config; |
| 7 | +use OhMyBrew\ShopifyApp\Models\Shop; |
| 8 | + |
| 9 | +/** |
| 10 | + * Responsible for managing webhooks. |
| 11 | + */ |
| 12 | +class WebhookManager |
| 13 | +{ |
| 14 | + /** |
| 15 | + * The shop. |
| 16 | + * |
| 17 | + * @var \OhMyBrew\ShopifyApp\Models\Shop |
| 18 | + */ |
| 19 | + protected $shop; |
| 20 | + |
| 21 | + /** |
| 22 | + * The shop API. |
| 23 | + * |
| 24 | + * @var \OhMyBrew\BasicShopifyAPI |
| 25 | + */ |
| 26 | + protected $api; |
| 27 | + |
| 28 | + /** |
| 29 | + * Cached shop webhooks result. |
| 30 | + * |
| 31 | + * @var array |
| 32 | + */ |
| 33 | + protected $shopWebhooks; |
| 34 | + |
| 35 | + /** |
| 36 | + * Create a new job instance. |
| 37 | + * |
| 38 | + * @param object $shop The shop object |
| 39 | + * |
| 40 | + * @return void |
| 41 | + */ |
| 42 | + public function __construct($shop) |
| 43 | + { |
| 44 | + $this->shop = $shop; |
| 45 | + $this->api = $this->shop->api(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Gets the webhooks present in the shop. |
| 50 | + * |
| 51 | + * @return array |
| 52 | + */ |
| 53 | + public function shopWebhooks() |
| 54 | + { |
| 55 | + if (!$this->shopWebhooks) { |
| 56 | + $this->shopWebhooks = $this->api->rest( |
| 57 | + 'GET', |
| 58 | + '/admin/webhooks.json', |
| 59 | + [ |
| 60 | + 'limit' => 250, |
| 61 | + 'fields' => 'id,address', |
| 62 | + ] |
| 63 | + )->body->webhooks; |
| 64 | + } |
| 65 | + |
| 66 | + return $this->shopWebhooks; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Gets the webhooks present in the configuration. |
| 71 | + * |
| 72 | + * @return array |
| 73 | + */ |
| 74 | + public function configWebhooks() |
| 75 | + { |
| 76 | + return Config::get('shopify-app.webhooks'); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Check if webhook is in the shop (by address). |
| 81 | + * |
| 82 | + * @param array $webhook The webhook |
| 83 | + * |
| 84 | + * @return bool |
| 85 | + */ |
| 86 | + public function webhookExists(array $webhook) |
| 87 | + { |
| 88 | + $shopWebhooks = $this->shopWebhooks(); |
| 89 | + foreach ($shopWebhooks as $shopWebhook) { |
| 90 | + if ($shopWebhook->address === $webhook['address']) { |
| 91 | + // Found the webhook in our list |
| 92 | + return true; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Creates webhooks (if they do not exist). |
| 101 | + * |
| 102 | + * @return array |
| 103 | + */ |
| 104 | + public function createWebhooks() |
| 105 | + { |
| 106 | + // Create if it does not exist |
| 107 | + $created = []; |
| 108 | + foreach ($configWebhooks as $webhook) { |
| 109 | + // Check if the required webhook exists on the shop |
| 110 | + if (!$this->webhookExists($webhook)) { |
| 111 | + // It does not... create the webhook |
| 112 | + $this->api->rest('POST', '/admin/webhooks.json', ['webhook' => $webhook]); |
| 113 | + $created[] = $webhook; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return $created; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Deletes webhooks in the shop tied to the app. |
| 122 | + * |
| 123 | + * @return array |
| 124 | + */ |
| 125 | + public function deleteWebhooks() |
| 126 | + { |
| 127 | + $shopWebhooks = $this->shopWebhooks(); |
| 128 | + |
| 129 | + $deleted = []; |
| 130 | + foreach ($shopWebhook as $webhook) { |
| 131 | + // Its a webhook in the config, delete it |
| 132 | + $this->api->rest('DELETE', "/admin/webhooks/${$webhook->id}.json"); |
| 133 | + $deleted[] = $webhook; |
| 134 | + } |
| 135 | + |
| 136 | + // Reset |
| 137 | + $this->shopWebhooks = null; |
| 138 | + |
| 139 | + return $deleted; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Recreates the webhooks. |
| 144 | + * |
| 145 | + * @return void |
| 146 | + */ |
| 147 | + public function recreateWebhooks() |
| 148 | + { |
| 149 | + $this->deleteWebhooks(); |
| 150 | + $this->createWebhooks(); |
| 151 | + } |
| 152 | +} |
0 commit comments