<?php
 namespace Shopify\Webhooks; abstract class DeliveryMethod { abstract public function getCallbackAddress(string $path): string; abstract protected function getMutationName(?string $webhookId): string; abstract protected function queryEndpoint(string $address): string; abstract public function buildCheckQuery(string $topic): string; abstract public function parseCheckQueryResult(array $body): array; public function buildRegisterQuery( string $topic, string $callbackAddress, ?string $webhookId = null ): string { $mutationName = $this->getMutationName($webhookId); $identifier = $webhookId ? "id: \"$webhookId\"" : "topic: $topic"; $webhookSubscriptionArgs = $this->queryEndpoint($callbackAddress); return <<<QUERY
        mutation webhookSubscription {
            $mutationName($identifier, webhookSubscription: $webhookSubscriptionArgs) {
                userErrors {
                    field
                    message
                }
                webhookSubscription {
                    id
                }
            }
        }
        QUERY;
} public function isSuccess(array $result, ?string $webhookId = null): bool { return !empty($result['data'][$this->getMutationName($webhookId)]['webhookSubscription']); } } 