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\WebhookExchangeRepository; |
8
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\WebhookQueueItem; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
10
|
|
|
use Symfony\Component\Console\Command\LockableTrait; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class EnqueueWebhooksCommand extends ContainerAwareCommand |
15
|
|
|
{ |
16
|
|
|
use LockableTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var OutputInterface |
20
|
|
|
*/ |
21
|
|
|
private $output; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var WebhookExchangeRepository |
25
|
|
|
*/ |
26
|
|
|
private $webhookExchangeRepository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EventRepository |
30
|
|
|
*/ |
31
|
|
|
private $eventRepository; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var SerializerInterface |
35
|
|
|
*/ |
36
|
|
|
private $serializer; |
37
|
|
|
|
38
|
|
|
public function __construct( |
39
|
|
|
WebhookExchangeRepository $webhookExchangeRepository, |
40
|
|
|
EventRepository $eventRepository, |
41
|
|
|
SerializerInterface $serializer |
42
|
|
|
) { |
43
|
|
|
$this->webhookExchangeRepository = $webhookExchangeRepository; |
44
|
|
|
$this->eventRepository = $eventRepository; |
45
|
|
|
$this->serializer = $serializer; |
46
|
|
|
|
47
|
|
|
parent::__construct(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function configure() |
51
|
|
|
{ |
52
|
|
|
$this->setName('loevgaard:dandomain:altapay:enqueue-webhooks') |
53
|
|
|
->setDescription('Will enqueue events to be sent to webhooks') |
54
|
|
|
; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
58
|
|
|
{ |
59
|
|
|
if (!($this->lock())) { |
60
|
|
|
$output->writeln('The command is already running in another process.'); |
61
|
|
|
|
62
|
|
|
return 0; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->output = $output; |
66
|
|
|
|
67
|
|
|
$maxEventsToEnqueue = 50; |
68
|
|
|
|
69
|
|
|
$webhookUrls = $this->getContainer()->getParameter('loevgaard_dandomain_altapay.webhook_urls'); |
70
|
|
|
if (empty($webhookUrls)) { |
71
|
|
|
$output->writeln('No webhook URLs defined'); |
72
|
|
|
|
73
|
|
|
$this->release(); |
74
|
|
|
|
75
|
|
|
return 0; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$eventsPerExchange = ceil($maxEventsToEnqueue / count($webhookUrls)); |
79
|
|
|
|
80
|
|
|
foreach ($webhookUrls as $webhookUrl) { |
81
|
|
|
$webhookExchange = $this->webhookExchangeRepository->findByUrlOrCreate($webhookUrl); |
82
|
|
|
|
83
|
|
|
$events = $this->eventRepository->findRecentEvents($webhookExchange->getLastEventId(), $eventsPerExchange); |
84
|
|
|
|
85
|
|
|
foreach ($events as $event) { |
|
|
|
|
86
|
|
|
$webhookQueueItem = new WebhookQueueItem($this->serializer->serialize($event, 'json'), $webhookExchange); |
87
|
|
|
|
88
|
|
|
$webhookExchange |
89
|
|
|
->addWebhookQueueItem($webhookQueueItem) |
90
|
|
|
->setLastEventId($event->getId()) |
91
|
|
|
; |
92
|
|
|
|
93
|
|
|
$this->webhookExchangeRepository->flush(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->release(); |
98
|
|
|
|
99
|
|
|
return 0; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.