Skip to content

Commit

Permalink
Merge pull request #6987 from pjeby/fix-6986-and-6594
Browse files Browse the repository at this point in the history
Fix #6986 - Immediate Webhooks broken on 2.15
  • Loading branch information
Woeler committed Dec 17, 2018
2 parents 8be7bea + 386e6a5 commit 23a0b3a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
45 changes: 14 additions & 31 deletions app/bundles/WebhookBundle/Model/WebhookModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ class WebhookModel extends FormModel
*/
protected $eventsOrderByDir;

/**
* @var array
*/
private $queuedPayloads = [];

/**
* WebhookModel constructor.
*
Expand Down Expand Up @@ -242,29 +237,19 @@ public function queueWebhooks($webhookEvents, $payload, array $serializationGrou
return;
}

$webhookList = [];

/** @var \Mautic\WebhookBundle\Entity\Event $event */
foreach ($webhookEvents as $event) {
$webhook = $event->getWebhook();
$webhookList[] = $webhook;

if (!isset($this->queuedPayloads[$webhook->getId()])) {
$this->queuedPayloads[$webhook->getId()] = [];
}
$this->queuedPayloads[$webhook->getId()] = $this->queueWebhook($webhook, $event, $payload, $serializationGroups);
$webhook = $event->getWebhook();
$queue = $this->queueWebhook($webhook, $event, $payload, $serializationGroups);

if (self::COMMAND_PROCESS === $this->queueMode) {
// Queue to the database to process later
$this->getQueueRepository()->saveEntity($this->queuedPayloads[$webhook->getId()]);
$this->getQueueRepository()->saveEntity($queue);
} else {
// Immediately process
$this->processWebhook($webhook, $queue);
}
}

if (self::IMMEDIATE_PROCESS === $this->queueMode) {
// Immediately process
$this->processWebhooks($webhookList);
$this->queuedPayloads = [];
}
}

/**
Expand Down Expand Up @@ -309,17 +294,18 @@ public function processWebhooks($webhooks)
}

/**
* @param Webhook $webhook
* @param Webhook $webhook
* @param WebhookQueue $queue
*
* @return bool
*/
public function processWebhook(Webhook $webhook)
public function processWebhook(Webhook $webhook, WebhookQueue $queue = null)
{
// instantiate new http class
$http = new Http();

// get the webhook payload
$payload = $this->getWebhookPayload($webhook);
$payload = $this->getWebhookPayload($webhook, $queue);

// if there wasn't a payload we can stop here
if (empty($payload)) {
Expand Down Expand Up @@ -506,11 +492,12 @@ public function getLogRepository()
/**
* Get the payload from the webhook.
*
* @param Webhook $webhook
* @param Webhook $webhook
* @param WebhookQueue $queue
*
* @return array
*/
public function getWebhookPayload(Webhook $webhook)
public function getWebhookPayload(Webhook $webhook, WebhookQueue $queue = null)
{
if ($payload = $webhook->getPayload()) {
return $payload;
Expand All @@ -521,7 +508,7 @@ public function getWebhookPayload(Webhook $webhook)
if ($this->queueMode === self::COMMAND_PROCESS) {
$queuesArray = $this->getWebhookQueues($webhook);
} else {
$queuesArray = isset($this->queuedPayloads[$webhook->getId()]) ? $this->queuedPayloads[$webhook->getId()] : [$webhook->getQueues()];
$queuesArray = [isset($queue) ? [$queue] : []];
}

/* @var WebhookQueue $queue */
Expand Down Expand Up @@ -549,10 +536,6 @@ public function getWebhookPayload(Webhook $webhook)

// Clear the WebhookQueue entity from memory
$this->em->detach($queue);
} else {
// remove the queue from the webhook right away so it won't get persisted to DB
// This happens on immediate send only
$webhook->removeQueue($queue);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions app/bundles/WebhookBundle/Tests/Model/WebhookModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ public function testGetWebhookPayloadForQueueInWebhook()
$queue->setPayload('{"the": "payload"}');
$queue->setEvent($event);
$queue->setDateAdded(new \DateTime('2018-04-10T15:04:57+00:00'));
$webhook->addQueue($queue);

$this->parametersHelperMock->expects($this->at(5))
->method('getParameter')
Expand All @@ -133,7 +132,7 @@ public function testGetWebhookPayloadForQueueInWebhook()
],
];

$this->assertEquals($expectedPayload, $this->initModel()->getWebhookPayload($webhook));
$this->assertEquals($expectedPayload, $this->initModel()->getWebhookPayload($webhook, $queue));
}

private function initModel()
Expand Down

0 comments on commit 23a0b3a

Please sign in to comment.