-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathDeliveryMethod.php
More file actions
117 lines (106 loc) · 3.11 KB
/
DeliveryMethod.php
File metadata and controls
117 lines (106 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace Shopify\Webhooks;
abstract class DeliveryMethod
{
/**
* Builds the full address to be used for the webhook, depending on the delivery method.
*
* @param string $path
*
* @return string
*/
abstract public function getCallbackAddress(string $path): string;
/**
* Builds the mutation name to be used depending on the delivery method and webhook id.
*
* @param string|null $webhookId
*
* @return string
*/
abstract protected function getMutationName(?string $webhookId): string;
/**
* Assembles a GraphQL query for registering a webhook.
*
* @param string $address
*
* @return string
*/
abstract protected function queryEndpoint(string $address): string;
/**
* Builds a GraphQL query to check whether this topic is already registered for the shop.
*
* @param string $topic
*
* @return string
*/
abstract public function buildCheckQuery(string $topic): string;
/**
* @param array $body
*
* @return array Array of the webhookId and current delivery address
*/
abstract public function parseCheckQueryResult(array $body): array;
/**
* Assembles a GraphQL query for registering a webhook.
*
* @param string $topic
* @param string $callbackAddress
* @param string|null $webhookId
*
* @return string
*/
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 buildDeleteQuery(
string $topic,
string $callbackAddress,
string $webhookId
): string {
$mutationName = $this->getDeletionMutationName();
$identifier = "id: \"$webhookId\"";
return <<<QUERY
mutation webhookSubscription {
$mutationName($identifier) {
userErrors {
field
message
}
deletedWebhookSubscriptionId
}
}
QUERY;
}
/**
* Checks if the given result was successful.
*
* @param array $result
* @param string|null $webhookId
*
* @return bool
*/
public function isSuccess(array $result, ?string $webhookId = null): bool
{
return !empty($result['data'][$this->getMutationName($webhookId)]['webhookSubscription'])
|| !empty($result['data'][$this->getDeletionMutationName()]['deletedWebhookSubscriptionId']);
}
}