vendor/dvdw/events/src/System/SalesChannel/Context/SalesChannelContextFactoryDecorator.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dvdw\Events\System\SalesChannel\Context;
  3. use Dvdw\Events\Content\DvdwEvent\DvdwEventCollection;
  4. use Dvdw\Events\Content\DvdwEvent\SalesChannel\AbstractDvdwEventRoute;
  5. use Dvdw\Events\Content\DvdwEvent\Struct\DvdwEventContextExtension;
  6. use Shopware\Core\Defaults;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  11. use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Symfony\Component\HttpFoundation\Request;
  14. class SalesChannelContextFactoryDecorator extends AbstractSalesChannelContextFactory
  15. {
  16.     private AbstractSalesChannelContextFactory $decorated;
  17.     private AbstractDvdwEventRoute $eventRoute;
  18.     public function __construct(
  19.         AbstractSalesChannelContextFactory $decorated,
  20.         AbstractDvdwEventRoute $eventRoute
  21.     )
  22.     {
  23.         $this->decorated $decorated;
  24.         $this->eventRoute $eventRoute;
  25.     }
  26.     public function getDecorated(): AbstractSalesChannelContextFactory
  27.     {
  28.         return $this->decorated;
  29.     }
  30.     public function create(string $tokenstring $salesChannelId, array $options = []): SalesChannelContext
  31.     {
  32.         $context $this->getDecorated()->create($token$salesChannelId$options);
  33.         $currentEvent $this->getEvents($context)->first();
  34.         if ($currentEvent !== null) {
  35.             $context->addExtension(DvdwEventContextExtension::KEY, new DvdwEventContextExtension($currentEvent));
  36.         }
  37.         return $context;
  38.     }
  39.     private function getEvents(SalesChannelContext $context): DvdwEventCollection
  40.     {
  41.         $criteria = new Criteria();
  42.         $criteria->addFilter(
  43.             new RangeFilter(
  44.                 'endDate',
  45.                 [RangeFilter::GTE => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)]
  46.             )
  47.         );
  48.         $criteria->addFilter(new EqualsAnyFilter('salesChannelIds', [$context->getSalesChannelId()]));
  49.         $criteria->addSorting(new FieldSorting('endDate'));
  50.         $criteria->setLimit(1);
  51.         $criteria->addAssociation('dvdwParticipations');
  52.         return $this->eventRoute->load(new Request(), $criteria$context)->getDvdwEvents();
  53.     }
  54. }