<?php
declare(strict_types=1);
namespace EconsorCrowdfunding\Subscriber;
use EconsorBundles\Services\BundleService;
use EconsorCrowdfunding\Models\Validation\ValidationEntity;
use EconsorCrowdfunding\Services\CrowdfundingMailService;
use Exception;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
/**
* Class ExcessCategorySubscriber
* @package EconsorCrowdfunding\Subscriber
*/
class ExcessCategorySubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $configService;
/**
* @var EntityRepositoryInterface
*/
private $categoryRepo;
/**
* @var EntityRepositoryInterface
*/
private $validationRepo;
/**
* @var Session
*/
private $session;
/**
* @var BundleService
*/
private $bundleService;
public function __construct(
SystemConfigService $configService,
EntityRepositoryInterface $category,
EntityRepositoryInterface $validation,
Session $session
/*BundleService $bundleService*/
) {
$this->configService = $configService;
$this->categoryRepo = $category;
$this->validationRepo = $validation;
$this->session = $session;
/*$this->bundleService = $bundleService;*/
}
public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => 'handleRequest'
];
}
/**
* @param StorefrontRenderEvent $event
* @throws Exception
*/
public function handleRequest(StorefrontRenderEvent $event)
{
$route = $event->getRequest()->attributes->get('navigationId');
$category = $this->configService->get('EconsorCrowdfunding.config.excessCategory');
$hash = $event->getRequest()->query->get('hash');
$currentUID = $event->getSalesChannelContext()->getCustomer() ? $event->getSalesChannelContext()->getCustomer()->getId() : '';
$validation = $this->getValidationEntry($hash);
$uID = $validation ? $validation->getCampaign()->getCustomer()->getId() : null;
if($route === $category)
{
if($validation && $currentUID === $uID)
{
//todo put bundle in WK
//todo set session variable that campaign is being finished
//todo add wk rebate of 10% per article added
$this->session->set('test', true);
dd($this->bundleService);
} else {
//throw new Exception('Access denied');
}
}
}
/**
* @param $hash
* @return ValidationEntity|null
*/
private function getValidationEntry($hash)
{
$c = new Criteria();
$c->addFilter(new EqualsFilter('hash', $hash));
return $this->validationRepo->search($c, Context::createDefaultContext())->getEntities()->first();
}
}