<?php declare(strict_types=1);
namespace Dvdw\Events\System\SalesChannel\Context;
use Dvdw\Events\Content\DvdwEvent\DvdwEventCollection;
use Dvdw\Events\Content\DvdwEvent\SalesChannel\AbstractDvdwEventRoute;
use Dvdw\Events\Content\DvdwEvent\Struct\DvdwEventContextExtension;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\Request;
class SalesChannelContextFactoryDecorator extends AbstractSalesChannelContextFactory
{
private AbstractSalesChannelContextFactory $decorated;
private AbstractDvdwEventRoute $eventRoute;
public function __construct(
AbstractSalesChannelContextFactory $decorated,
AbstractDvdwEventRoute $eventRoute
)
{
$this->decorated = $decorated;
$this->eventRoute = $eventRoute;
}
public function getDecorated(): AbstractSalesChannelContextFactory
{
return $this->decorated;
}
public function create(string $token, string $salesChannelId, array $options = []): SalesChannelContext
{
$context = $this->getDecorated()->create($token, $salesChannelId, $options);
$currentEvent = $this->getEvents($context)->first();
if ($currentEvent !== null) {
$context->addExtension(DvdwEventContextExtension::KEY, new DvdwEventContextExtension($currentEvent));
}
return $context;
}
private function getEvents(SalesChannelContext $context): DvdwEventCollection
{
$criteria = new Criteria();
$criteria->addFilter(
new RangeFilter(
'endDate',
[RangeFilter::GTE => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)]
)
);
$criteria->addFilter(new EqualsAnyFilter('salesChannelIds', [$context->getSalesChannelId()]));
$criteria->addSorting(new FieldSorting('endDate'));
$criteria->setLimit(1);
$criteria->addAssociation('dvdwParticipations');
return $this->eventRoute->load(new Request(), $criteria, $context)->getDvdwEvents();
}
}