|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodeCloud\Bundle\ShopifyBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use CodeCloud\Bundle\ShopifyBundle\Event\WebhookEvent; |
|
6
|
|
|
use CodeCloud\Bundle\ShopifyBundle\Model\ShopifyStoreManagerInterface; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
11
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
12
|
|
|
|
|
13
|
|
|
class WebhookController |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ShopifyStoreManagerInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $storeManager; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var EventDispatcherInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $eventDispatcher; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param ShopifyStoreManagerInterface $storeManager |
|
27
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(ShopifyStoreManagerInterface $storeManager, EventDispatcherInterface $eventDispatcher) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->storeManager = $storeManager; |
|
32
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Request $request |
|
37
|
|
|
* @return Response |
|
38
|
|
|
*/ |
|
39
|
|
|
public function handleWebhook(Request $request) |
|
40
|
|
|
{ |
|
41
|
|
|
$topic = $request->query->get('topic'); |
|
42
|
|
|
$storeName = $request->query->get('store'); |
|
43
|
|
|
|
|
44
|
|
|
if (!$topic || !$storeName) { |
|
45
|
|
|
throw new NotFoundHttpException(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (!$this->storeManager->storeExists($storeName)) { |
|
49
|
|
|
throw new NotFoundHttpException(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (empty($request->getContent())) { |
|
53
|
|
|
// todo log! |
|
54
|
|
|
throw new BadRequestHttpException('Webhook must have body content'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$payload = \GuzzleHttp\json_decode($request->getContent(), true); |
|
58
|
|
|
|
|
59
|
|
|
$this->eventDispatcher->dispatch(WebhookEvent::NAME, new WebhookEvent( |
|
60
|
|
|
$topic, |
|
61
|
|
|
$storeName, |
|
62
|
|
|
$payload |
|
63
|
|
|
)); |
|
64
|
|
|
|
|
65
|
|
|
return new Response('Shopify Webhook Received'); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|