Passed
Pull Request — master (#9)
by Daniel
03:07
created

WebhookCreatorEventBridge   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 22
c 1
b 0
f 1
dl 0
loc 63
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteAllWebhooks() 0 6 2
A __construct() 0 4 1
A listWebhooks() 0 5 1
A createWebhooks() 0 19 4
1
<?php
2
3
namespace CodeCloud\Bundle\ShopifyBundle\Service;
4
5
use CodeCloud\Bundle\ShopifyBundle\Api\GenericResource;
6
use CodeCloud\Bundle\ShopifyBundle\Api\ShopifyApiFactory;
7
use GuzzleHttp\Exception\ClientException;
8
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
9
10
/**
11
 * Creates EventBridge Webhooks.
12
 */
13
class WebhookCreatorEventBridge implements WebhookCreatorInterface
14
{
15
    /**
16
     * @var ShopifyApiFactory
17
     */
18
    private $apis;
19
20
    /**
21
     * @var string
22
     */
23
    private $eventBridgeArn;
24
25
    public function __construct(ShopifyApiFactory $apis, string $eventBridgeArn)
26
    {
27
        $this->apis = $apis;
28
        $this->eventBridgeArn = $eventBridgeArn;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function createWebhooks(string $storeName, array $topics)
35
    {
36
        $api = $this->apis->getForStore($storeName);
37
38
        foreach ($topics as $topic) {
39
            $webhook = GenericResource::create([
40
                'topic' => $topic,
41
                'address' => $this->eventBridgeArn,
42
                'format' => 'json',
43
            ]);
44
45
            try {
46
                $api->Webhook->create($webhook);
47
            } catch (ClientException $e) {
48
                if ($e->getResponse()->getStatusCode() == 422) {
49
                    continue;
50
                }
51
52
                throw $e;
53
            }
54
        }
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function listWebhooks(string $storeName)
61
    {
62
        $api = $this->apis->getForStore($storeName);
63
64
        return $api->Webhook->findAll();
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function deleteAllWebhooks(string $storeName)
71
    {
72
        $api = $this->apis->getForStore($storeName);
73
74
        foreach ($api->Webhook->findAll() as $webhook) {
75
            $api->Webhook->delete($webhook['id']);
76
        }
77
    }
78
}
79