Passed
Pull Request — master (#9)
by Daniel
02:56
created

WebhookCreatorLocal   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createWebhooks() 0 20 2
A __construct() 0 5 1
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 Webhooks to be received by this application.
12
 */
13
class WebhookCreatorLocal extends AbstractWebhookCreator
14
{
15
    /**
16
     * @var UrlGeneratorInterface
17
     */
18
    private $router;
19
20
    /**
21
     * @param ShopifyApiFactory $apis
22
     * @param UrlGeneratorInterface $router
23
     */
24
    public function __construct(ShopifyApiFactory $apis, UrlGeneratorInterface $router)
25
    {
26
        $this->router = $router;
27
28
        parent::__construct($apis);
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
            $endpoint = $this->router->generate('codecloud_shopify_webhooks', [
40
                'store' => $storeName,
41
                'topic' => $topic,
42
            ], UrlGeneratorInterface::ABSOLUTE_URL);
43
44
            // endpoint HAS to be https
45
            $endpoint = str_replace("http://", "https://", $endpoint);
46
47
            $webhook = GenericResource::create([
48
                'topic' => $topic,
49
                'address' => $endpoint,
50
                'format' => 'json',
51
            ]);
52
53
            $api->Webhook->create($webhook);
54
        }
55
    }
56
}
57