Skip to content

Commit

Permalink
Only allow one conversion per order
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Oct 10, 2023
1 parent 84b4877 commit 80d015c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/EventListener/PurchaseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Setono\DoctrineObjectManagerTrait\ORM\ORMManagerTrait;
use Setono\SyliusGoogleAdsPlugin\Event\PrePersistConversionFromOrderEvent;
use Setono\SyliusGoogleAdsPlugin\Factory\ConversionFactoryInterface;
use Setono\SyliusGoogleAdsPlugin\Repository\ConversionRepositoryInterface;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Sylius\Component\Core\Model\OrderInterface;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -26,6 +27,7 @@ final class PurchaseListener implements LoggerAwareInterface
public function __construct(
ManagerRegistry $managerRegistry,
private readonly ConversionFactoryInterface $conversionFactory,
private readonly ConversionRepositoryInterface $conversionRepository,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly RequestStack $requestStack,
private readonly string $cookieName,
Expand All @@ -42,6 +44,10 @@ public function track(ResourceControllerEvent $event): void
$order = $event->getSubject();
Assert::isInstanceOf($order, OrderInterface::class);

if (null !== $this->conversionRepository->findOneByOrder($order)) {
return;
}

$request = $this->requestStack->getMainRequest();
Assert::notNull($request);

Expand Down
17 changes: 17 additions & 0 deletions src/Repository/ConversionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
namespace Setono\SyliusGoogleAdsPlugin\Repository;

use Doctrine\ORM\QueryBuilder;
use Setono\SyliusGoogleAdsPlugin\Model\ConversionInterface;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Component\Order\Model\OrderInterface;
use Webmozart\Assert\Assert;

class ConversionRepository extends EntityRepository implements ConversionRepositoryInterface
{
Expand All @@ -31,4 +34,18 @@ public function prune(): int
->execute()
;
}

public function findOneByOrder(OrderInterface $order): ?ConversionInterface
{
$obj = $this->createQueryBuilder('o')
->andWhere('o.order = :order')
->setParameter('order', $order)
->getQuery()
->getOneOrNullResult()
;

Assert::nullOrIsInstanceOf($obj, ConversionInterface::class);

return $obj;
}
}
3 changes: 3 additions & 0 deletions src/Repository/ConversionRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\ORM\QueryBuilder;
use Setono\SyliusGoogleAdsPlugin\Model\ConversionInterface;
use Sylius\Component\Order\Model\OrderInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

/**
Expand All @@ -21,4 +22,6 @@ public function createPreQualifiedConversionQueryBuilder(string $alias = 'o'): Q
* @return int the number of conversions pruned/removed
*/
public function prune(): int;

public function findOneByOrder(OrderInterface $order): ?ConversionInterface;
}
4 changes: 2 additions & 2 deletions src/Resources/config/doctrine/model/Conversion.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
<join-column name="channel_id" referenced-column-name="id" on-delete="CASCADE" nullable="false"/>
</many-to-one>

<many-to-one field="order" target-entity="Sylius\Component\Order\Model\OrderInterface">
<one-to-one field="order" target-entity="Sylius\Component\Order\Model\OrderInterface">
<join-column name="order_id" referenced-column-name="id" on-delete="SET NULL"/>
</many-to-one>
</one-to-one>
</mapped-superclass>
</doctrine-mapping>
1 change: 1 addition & 0 deletions src/Resources/config/services/event_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class="Setono\SyliusGoogleAdsPlugin\EventListener\PurchaseListener">
<argument type="service" id="doctrine"/>
<argument type="service" id="setono_sylius_google_ads.factory.conversion"/>
<argument type="service" id="setono_sylius_google_ads.repository.conversion"/>
<argument type="service" id="event_dispatcher"/>
<argument type="service" id="request_stack"/>
<argument>%setono_sylius_google_ads.gclid_cookie_name%</argument>
Expand Down

0 comments on commit 80d015c

Please sign in to comment.