Skip to content

Commit

Permalink
Segment delegate decorator event (#10890)
Browse files Browse the repository at this point in the history
* make sub-querybuilders pass paramater types along and allow to bypass decorator to get crate value

* cs fixes?

* tests for dynamic content

* update reference to CO plugin

* update reference co bundle, fix cs

* fix tests, cover event dispatch with test

* remove use statements from config

* replace int value with constant

* add argument removed on cleanup

* add helper method

* Fixes after wild cherry-pick conflict resolutions of antient commits

* New PHP syntax suggar

* CS fix

* Improving tests

* CS fix

* CS fix

* CS fix

Co-authored-by: Jan Kozak <galvani78@gmail.com>
Co-authored-by: Ruth Cheesley <ruth.cheesley@acquia.com>
  • Loading branch information
3 people committed Mar 22, 2022
1 parent d4724dd commit 2701926
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 63 deletions.
5 changes: 3 additions & 2 deletions app/bundles/LeadBundle/Config/config.php
Expand Up @@ -380,8 +380,8 @@
'mautic.lead.campaignbundle.action_delete_contacts.subscriber' => [
'class' => \Mautic\LeadBundle\EventListener\CampaignActionDeleteContactSubscriber::class,
'arguments' => [
'mautic.lead.model.lead',
'mautic.campaign.helper.removed_contact_tracker',
'mautic.lead.model.lead',
'mautic.campaign.helper.removed_contact_tracker',
],
],
'mautic.lead.campaignbundle.action_dnc.subscriber' => [
Expand Down Expand Up @@ -1219,6 +1219,7 @@
'mautic.lead.model.lead_segment_decorator_custom_mapped',
'mautic.lead.model.lead_segment.decorator.date.optionFactory',
'mautic.lead.model.lead_segment_decorator_company',
'event_dispatcher',
],
],
'mautic.lead.model.lead_segment_decorator_base' => [
Expand Down
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Mautic\LeadBundle\Event;

use Mautic\CoreBundle\Event\CommonEvent;
use Mautic\LeadBundle\Segment\ContactSegmentFilterCrate;
use Mautic\LeadBundle\Segment\Decorator\FilterDecoratorInterface;

final class LeadListFiltersDecoratorDelegateEvent extends CommonEvent
{
private ?FilterDecoratorInterface $decorator = null;
private ContactSegmentFilterCrate $crate;

public function __construct(ContactSegmentFilterCrate $crate)
{
$this->crate = $crate;
}

public function getDecorator(): ?FilterDecoratorInterface
{
return $this->decorator;
}

public function setDecorator(FilterDecoratorInterface $decorator): self
{
$this->decorator = $decorator;

return $this;
}

public function getCrate(): ContactSegmentFilterCrate
{
return $this->crate;
}
}
Expand Up @@ -46,4 +46,9 @@ public function getTranslations()
{
return $this->translations;
}

public function hasTranslation(string $key): bool
{
return isset($this->translations[$key]);
}
}
5 changes: 3 additions & 2 deletions app/bundles/LeadBundle/Segment/ContactSegmentFilter.php
Expand Up @@ -2,6 +2,7 @@

namespace Mautic\LeadBundle\Segment;

use Doctrine\DBAL\Schema\Column;
use Mautic\LeadBundle\Segment\Decorator\FilterDecoratorInterface;
use Mautic\LeadBundle\Segment\DoNotContact\DoNotContactParts;
use Mautic\LeadBundle\Segment\Exception\FieldNotFoundException;
Expand Down Expand Up @@ -47,7 +48,7 @@ public function __construct(
}

/**
* @return \Doctrine\DBAL\Schema\Column
* @return Column
*
* @throws FieldNotFoundException
*/
Expand Down Expand Up @@ -210,7 +211,7 @@ public function __toString()
{
return sprintf(
'table: %s, %s on %s %s %s',
$this->getTable(),
$this->getTable(),
$this->getField(),
$this->getQueryType(),
$this->getOperator(),
Expand Down
5 changes: 5 additions & 0 deletions app/bundles/LeadBundle/Segment/ContactSegmentFilterCrate.php
Expand Up @@ -199,4 +199,9 @@ public function getNullValue()
{
return $this->nullValue;
}

public function getObject(): ?string
{
return $this->object;
}
}
Expand Up @@ -2,6 +2,7 @@

namespace Mautic\LeadBundle\Segment;

use Exception;
use Mautic\LeadBundle\Entity\LeadList;
use Mautic\LeadBundle\Segment\Decorator\DecoratorFactory;
use Mautic\LeadBundle\Segment\Decorator\FilterDecoratorInterface;
Expand All @@ -25,9 +26,6 @@ class ContactSegmentFilterFactory
*/
private $decoratorFactory;

/**
* ContactSegmentFilterFactory constructor.
*/
public function __construct(
TableSchemaColumnsCache $schemaCache,
Container $container,
Expand All @@ -41,7 +39,7 @@ public function __construct(
/**
* @return ContactSegmentFilters
*
* @throws \Exception
* @throws Exception
*/
public function getSegmentFilters(LeadList $leadList)
{
Expand Down Expand Up @@ -76,7 +74,7 @@ public function factorSegmentFilter(array $filter)
/**
* @return FilterQueryBuilderInterface
*
* @throws \Exception
* @throws Exception
*/
private function getQueryBuilderForFilter(FilterDecoratorInterface $decorator, ContactSegmentFilterCrate $contactSegmentFilterCrate)
{
Expand Down
2 changes: 1 addition & 1 deletion app/bundles/LeadBundle/Segment/ContactSegmentService.php
Expand Up @@ -248,7 +248,7 @@ private function getOrphanedLeadListLeadsQueryBuilder(LeadList $segment, array $
$qbO->select('orp.lead_id as id, orp.leadlist_id')
->from(MAUTIC_TABLE_PREFIX.'lead_lists_leads', 'orp');
$qbO->leftJoin('orp', '('.$queryBuilder->getSQL().')', 'members', 'members.id=orp.lead_id');
$qbO->setParameters($queryBuilder->getParameters());
$qbO->setParameters($queryBuilder->getParameters(), $queryBuilder->getParameterTypes());
$qbO->andWhere($qbO->expr()->eq('orp.leadlist_id', ':orpsegid'));
$qbO->andWhere($qbO->expr()->isNull('members.id'));
$qbO->andWhere($qbO->expr()->eq('orp.manually_added', $qbO->expr()->literal(0)));
Expand Down
21 changes: 16 additions & 5 deletions app/bundles/LeadBundle/Segment/Decorator/DecoratorFactory.php
Expand Up @@ -2,14 +2,14 @@

namespace Mautic\LeadBundle\Segment\Decorator;

use Mautic\LeadBundle\Event\LeadListFiltersDecoratorDelegateEvent;
use Mautic\LeadBundle\Exception\FilterNotFoundException;
use Mautic\LeadBundle\LeadEvents;
use Mautic\LeadBundle\Segment\ContactSegmentFilterCrate;
use Mautic\LeadBundle\Segment\Decorator\Date\DateOptionFactory;
use Mautic\LeadBundle\Services\ContactSegmentFilterDictionary;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Class DecoratorFactory.
*/
class DecoratorFactory
{
/**
Expand Down Expand Up @@ -38,27 +38,38 @@ class DecoratorFactory
private $dateOptionFactory;

/**
* DecoratorFactory constructor.
* @var EventDispatcherInterface
*/
private $eventDispatcher;

public function __construct(
ContactSegmentFilterDictionary $contactSegmentFilterDictionary,
BaseDecorator $baseDecorator,
CustomMappedDecorator $customMappedDecorator,
DateOptionFactory $dateOptionFactory,
CompanyDecorator $companyDecorator
CompanyDecorator $companyDecorator,
EventDispatcherInterface $eventDispatcher
) {
$this->baseDecorator = $baseDecorator;
$this->customMappedDecorator = $customMappedDecorator;
$this->dateOptionFactory = $dateOptionFactory;
$this->contactSegmentFilterDictionary = $contactSegmentFilterDictionary;
$this->companyDecorator = $companyDecorator;
$this->eventDispatcher = $eventDispatcher;
}

/**
* @return FilterDecoratorInterface
*/
public function getDecoratorForFilter(ContactSegmentFilterCrate $contactSegmentFilterCrate)
{
$decoratorEvent = new LeadListFiltersDecoratorDelegateEvent($contactSegmentFilterCrate);

$this->eventDispatcher->dispatch(LeadEvents::SEGMENT_ON_DECORATOR_DELEGATE, $decoratorEvent);
if ($decorator = $decoratorEvent->getDecorator()) {
return $decorator;
}

if ($contactSegmentFilterCrate->isDateType()) {
$dateDecorator = $this->dateOptionFactory->getDateOption($contactSegmentFilterCrate);

Expand Down
Expand Up @@ -129,7 +129,8 @@ public function wrapInCount(QueryBuilder $qb)

$queryBuilder->select('count(leadIdPrimary) count, max(leadIdPrimary) maxId, min(leadIdPrimary) minId')
->from('('.$qb->getSQL().')', 'sss');
$queryBuilder->setParameters($qb->getParameters());

$queryBuilder->setParameters($qb->getParameters(), $qb->getParameterTypes());

return $queryBuilder;
}
Expand Down
7 changes: 6 additions & 1 deletion app/bundles/LeadBundle/Segment/Query/QueryBuilder.php
Expand Up @@ -1593,8 +1593,13 @@ public function getDebugOutput()
$params = $this->getParameters();
$sql = $this->getSQL();
foreach ($params as $key=>$val) {
if (!is_int($val) and !is_float($val)) {
if (!is_int($val) && !is_float($val) && !is_array($val)) {
$val = "'$val'";
} elseif (is_array($val)) {
if (Connection::PARAM_STR_ARRAY === $this->getParameterType($key)) {
$val = array_map(fn ($value) => "'$value'", $val);
}
$val = join(', ', $val);
}
$sql = str_replace(":{$key}", $val, $sql);
}
Expand Down

0 comments on commit 2701926

Please sign in to comment.