Skip to content

Commit

Permalink
Merge pull request mautic#13086 from dadarya0/remove_unnecessary_quer…
Browse files Browse the repository at this point in the history
…ies_to_integration_entity_table

Remove unnecessary queries to integration entity table
  • Loading branch information
escopecz authored Jan 17, 2024
2 parents c6495c9 + 944249d commit 04eabd6
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 4 deletions.
31 changes: 27 additions & 4 deletions app/bundles/PluginBundle/EventListener/LeadSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

namespace Mautic\PluginBundle\EventListener;

use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Event\CompanyEvent;
use Mautic\LeadBundle\Event\LeadEvent;
use Mautic\LeadBundle\LeadEvents;
use Mautic\PluginBundle\Entity\Integration;
use Mautic\PluginBundle\Entity\IntegrationRepository;
use Mautic\PluginBundle\Model\PluginModel;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class LeadSubscriber implements EventSubscriberInterface
{
private const FEATURE_PUSH_LEAD = 'push_lead';

public function __construct(
private PluginModel $pluginModel
private PluginModel $pluginModel,
private IntegrationRepository $integrationRepository
) {
}

Expand All @@ -29,7 +35,7 @@ public static function getSubscribedEvents(): array
*/
public function onLeadDelete(LeadEvent $event): bool
{
/** @var \Mautic\LeadBundle\Entity\Lead $lead */
/** @var Lead $lead */
$lead = $event->getLead();
$integrationEntityRepo = $this->pluginModel->getIntegrationEntityRepository();
$integrationEntityRepo->findLeadsToDelete('lead%', $lead->getId());
Expand All @@ -55,9 +61,26 @@ public function onCompanyDelete(CompanyEvent $event): bool
*/
public function onLeadSave(LeadEvent $event): void
{
/** @var \Mautic\LeadBundle\Entity\Lead $lead */
/** @var Lead $lead */
$lead = $event->getLead();
$integrationEntityRepo = $this->pluginModel->getIntegrationEntityRepository();
$integrationEntityRepo->updateErrorLeads('lead-error', $lead->getId());
if ($this->isAnyIntegrationEnabled()) {
$integrationEntityRepo->updateErrorLeads('lead-error', $lead->getId());
}
}

private function isAnyIntegrationEnabled(): bool
{
$integrations = $this->integrationRepository->getIntegrations();
foreach ($integrations as $integration) {
/** @var Integration $integration */
$supportedFeatures = $integration->getSupportedFeatures();

if ($integration->getIsPublished() && !empty($integration->getApiKeys()) && in_array(self::FEATURE_PUSH_LEAD, $supportedFeatures)) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Mautic\PluginBundle\Tests\EventListener;

use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Event\LeadEvent;
use Mautic\PluginBundle\Entity\Integration;
use Mautic\PluginBundle\Entity\IntegrationEntityRepository;
use Mautic\PluginBundle\Entity\IntegrationRepository;
use Mautic\PluginBundle\EventListener\LeadSubscriber;
use Mautic\PluginBundle\Model\PluginModel;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class LeadSubscriberTest extends TestCase
{
private LeadSubscriber $subscriber;

/**
* @var LeadEvent|MockObject
*/
private $leadEvent;

/**
* @var IntegrationEntityRepository|MockObject
*/
private $integrationEntityRepository;

/**
* @var IntegrationRepository|MockObject
*/
private $integrationRepository;

protected function setUp(): void
{
$pluginModel = $this->createMock(PluginModel::class);
$this->integrationRepository = $this->createMock(IntegrationRepository::class);
$this->leadEvent = $this->createMock(LeadEvent::class);
$this->integrationEntityRepository = $this->createMock(IntegrationEntityRepository::class);
$this->subscriber = new LeadSubscriber(
$pluginModel,
$this->integrationRepository
);

$pluginModel->expects($this->once())
->method('getIntegrationEntityRepository')
->willReturn($this->integrationEntityRepository);

$this->leadEvent->expects($this->once())
->method('getLead')
->willReturn(new Lead());
}

public function testOnLeadSaveWithoutActiveIntegration(): void
{
$this->integrationRepository->expects($this->once())
->method('getIntegrations')
->willReturn([]);

$this->integrationEntityRepository->expects($this->never())
->method('updateErrorLeads');

$this->subscriber->onLeadSave($this->leadEvent);
}

public function testOnLeadSaveWithActiveIntegration(): void
{
$integration = new Integration();
$integration->setIsPublished(true);
$integration->setApiKeys(['key' => 'some']);
$integration->setSupportedFeatures(['push_lead']);

$this->integrationRepository->expects($this->once())
->method('getIntegrations')
->willReturn([$integration]);

$this->integrationEntityRepository->expects($this->once())
->method('updateErrorLeads');

$this->subscriber->onLeadSave($this->leadEvent);
}
}

0 comments on commit 04eabd6

Please sign in to comment.