|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainAltapayBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use JMS\Serializer\SerializerInterface; |
|
6
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\EventRepository; |
|
7
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\WebhookExchange; |
|
8
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\WebhookExchangeRepository; |
|
9
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\WebhookQueueItem; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
11
|
|
|
use Symfony\Component\Console\Command\LockableTrait; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
|
|
15
|
|
|
class EnqueueWebhooksCommand extends ContainerAwareCommand |
|
16
|
|
|
{ |
|
17
|
|
|
use LockableTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var OutputInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $output; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var WebhookExchangeRepository |
|
26
|
|
|
*/ |
|
27
|
|
|
private $webhookExchangeRepository; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var EventRepository |
|
31
|
|
|
*/ |
|
32
|
|
|
private $eventRepository; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var SerializerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $serializer; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct( |
|
40
|
|
|
WebhookExchangeRepository $webhookExchangeRepository, |
|
41
|
|
|
EventRepository $eventRepository, |
|
42
|
|
|
SerializerInterface $serializer |
|
43
|
|
|
) { |
|
44
|
|
|
$this->webhookExchangeRepository = $webhookExchangeRepository; |
|
45
|
|
|
$this->eventRepository = $eventRepository; |
|
46
|
|
|
$this->serializer = $serializer; |
|
47
|
|
|
|
|
48
|
|
|
parent::__construct(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function configure() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->setName('loevgaard:dandomain:altapay:enqueue-webhooks') |
|
54
|
|
|
->setDescription('Will enqueue events to be sent to webhooks') |
|
55
|
|
|
; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!($this->lock())) { |
|
61
|
|
|
$output->writeln('The command is already running in another process.'); |
|
62
|
|
|
|
|
63
|
|
|
return 0; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->output = $output; |
|
67
|
|
|
|
|
68
|
|
|
$maxEventsToEnqueue = 50; |
|
69
|
|
|
|
|
70
|
|
|
$webhookUrls = $this->getContainer()->getParameter('loevgaard_dandomain_altapay.webhook_urls'); |
|
71
|
|
|
if (empty($webhookUrls)) { |
|
72
|
|
|
$output->writeln('No webhook URLs defined'); |
|
73
|
|
|
|
|
74
|
|
|
return 0; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** @var WebhookExchange[] $webhookExchanges */ |
|
78
|
|
|
$webhookExchanges = []; |
|
79
|
|
|
|
|
80
|
|
|
foreach ($webhookUrls as $webhookUrl) { |
|
81
|
|
|
$webhookExchange = $this->webhookExchangeRepository->findByUrlOrCreate($webhookUrl); |
|
82
|
|
|
$webhookExchanges[] = $webhookExchange; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$eventsPerExchange = ceil($maxEventsToEnqueue / count($webhookExchanges)); |
|
86
|
|
|
|
|
87
|
|
|
foreach ($webhookExchanges as $webhookExchange) { |
|
88
|
|
|
$events = $this->eventRepository->findRecentEvents($webhookExchange->getLastEventId(), $eventsPerExchange); |
|
89
|
|
|
|
|
90
|
|
|
foreach ($events as $event) { |
|
|
|
|
|
|
91
|
|
|
$webhookQueueItem = new WebhookQueueItem($this->serializer->serialize($event, 'json'), $webhookExchange); |
|
92
|
|
|
|
|
93
|
|
|
$webhookExchange |
|
94
|
|
|
->addWebhookQueueItem($webhookQueueItem) |
|
95
|
|
|
->setLastEventId($event->getId()) |
|
96
|
|
|
; |
|
97
|
|
|
|
|
98
|
|
|
$this->webhookExchangeRepository->flush(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return 0; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.