-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathPubSub.php
More file actions
92 lines (82 loc) · 2.38 KB
/
PubSub.php
File metadata and controls
92 lines (82 loc) · 2.38 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
<?php
namespace Shopify\Webhooks\Delivery;
use Shopify\Context;
use Shopify\Exception\InvalidArgumentException;
use Shopify\Utils;
use Shopify\Webhooks\DeliveryMethod;
class PubSub extends DeliveryMethod
{
/**
* @throws InvalidArgumentException
*/
public function __construct()
{
if (!Utils::isApiVersionCompatible('2021-07')) {
throw new InvalidArgumentException(
"PubSub webhooks are not supported in API version " . Context::$API_VERSION
);
}
}
/**
* @inheritDoc
*/
public function getCallbackAddress(string $path): string
{
return $path;
}
/**
* @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'];
$currentAddress = "pubsub://" . $node['endpoint']['pubSubProject'] . ":" . $node['endpoint']['pubSubTopic'];
}
return [$webhookId, $currentAddress];
}
/**
* @inheritDoc
*/
public function buildCheckQuery(string $topic): string
{
return <<<QUERY
{
webhookSubscriptions(first: 1, topics: $topic) {
edges {
node {
id
endpoint {
__typename
... on WebhookPubSubEndpoint {
pubSubProject
pubSubTopic
}
}
}
}
}
}
QUERY;
}
/**
* @inheritDoc
*/
protected function getMutationName(?string $webhookId): string
{
return $webhookId ? "pubSubWebhookSubscriptionUpdate" : "pubSubWebhookSubscriptionCreate";
}
/**
* @inheritDoc
*/
protected function queryEndpoint(string $address): string
{
$addressWithoutProtocol = explode("//", $address);
list($project, $topic) = explode(":", $addressWithoutProtocol[1]);
return "pubSubProject: \"$project\", pubSubTopic: \"$topic\"";
}
}