-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathHttpDelivery.php
More file actions
99 lines (92 loc) · 2.59 KB
/
HttpDelivery.php
File metadata and controls
99 lines (92 loc) · 2.59 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
<?php
namespace Shopify\Webhooks\Delivery;
use Shopify\Exception\InvalidArgumentException;
use Shopify\Context;
use Shopify\Utils;
use Shopify\Webhooks\DeliveryMethod;
class HttpDelivery extends DeliveryMethod
{
/**
* @inheritDoc
*/
public function getCallbackAddress(string $path): string
{
return 'https://' . Context::$HOST_NAME . '/' . ltrim($path, '/');
}
/**
* Builds a GraphQL query to check whether this topic is already registered for the shop.
*
* @param string $topic
*
* @return string
* @throws InvalidArgumentException
*/
public function buildCheckQuery(string $topic): string
{
if (Utils::isApiVersionCompatible('2020-07')) {
return <<<QUERY
{
webhookSubscriptions(first: 1, topics: $topic) {
edges {
node {
id
endpoint {
__typename
... on WebhookHttpEndpoint {
callbackUrl
}
}
}
}
}
}
QUERY;
} else {
return <<<LEGACY_QUERY
{
webhookSubscriptions(first: 1, topics: $topic) {
edges {
node {
id
callbackUrl
}
}
}
}
LEGACY_QUERY;
}
}
/**
* @inheritDoc
*/
public function parseCheckQueryResult(array $body): array
{
$edges = $body['data']['webhookSubscriptions']['edges'] ?? [];
$webhookId = null;
$currentAddress = null;
if (count($edges ?? [])) {
$node = $edges[0]['node'];
$webhookId = (string)$node['id'];
if (array_key_exists('endpoint', $node)) {
$currentAddress = $node['endpoint']['callbackUrl'];
} else {
$currentAddress = $node['callbackUrl'];
}
}
return [$webhookId, $currentAddress];
}
/**
* @inheritDoc
*/
protected function getMutationName(?string $webhookId): string
{
return $webhookId ? "webhookSubscriptionUpdate" : "webhookSubscriptionCreate";
}
/**
* @inheritDoc
*/
protected function queryEndpoint(string $address): string
{
return "callbackUrl: \"$address\"";
}
}