<?php declare(strict_types=1);
namespace WabsAuth\Subscriber;
use Acris\CustomerGroupAvailableProduct\Components\DisplayAllowedProductService;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
class ProductSubscriber implements EventSubscriberInterface
{
/**
* @var DisplayAllowedProductService
*/
private $displayProductService;
/**
* @var EntityRepositoryInterface
*/
private $productCustomerGroupRepository;
/**
* @var EntityRepositoryInterface
*/
private $customerGroupRepository;
/**
* @var SystemConfigService
*/
private $configService;
/**
* @var RouterInterface
*/
private $router;
public function __construct(DisplayAllowedProductService $displayProductService, EntityRepositoryInterface $productCustomerGroupRepository, EntityRepositoryInterface $customerGroupRepository, SystemConfigService $configService, RouterInterface $router)
{
$this->displayProductService = $displayProductService;
$this->productCustomerGroupRepository = $productCustomerGroupRepository;
$this->customerGroupRepository = $customerGroupRepository;
$this->configService = $configService;
$this->router = $router;
}
public static function getSubscribedEvents(): array
{
return[
ProductPageLoadedEvent::class => ['onProductLoaded', 9999]
];
}
public function onProductLoaded(ProductPageLoadedEvent $event)
{
if( !$event->getRequest()->get('checkLogin') || $event->getSalesChannelContext()->getCustomerId()) {
return;
}
$productId = $event->getPage()->getProduct()->getId();
if(empty($productId)) {
return;
}
if($this->configService->get('AcrisCustomerGroupAvailableProductCS.config.releaseProductsIfNoCustomerGroupAssigned', $event->getSalesChannelContext()->getSalesChannel()->getId()) === DisplayAllowedProductService::DEFAULT_PLUGIN_CONFIG_RELEASE_PRODUCT_IF_NO_CUSTOMER_GROUPS_ASSIGNED && $this->displayProductService->checkIfNoCustomerGroupsAssigned($productId, $event->getSalesChannelContext()->getContext())) {
return;
}
$displayProductIdsResult = $this->productCustomerGroupRepository->searchIds(
(new Criteria())
->addFilter(new EqualsFilter('productId', $productId))
->addAssociation('customerGroup')
, $event->getContext()
);
if($displayProductIdsResult) {
$displayIds = $displayProductIdsResult->getIds();
$customerGroupIds = [];
foreach ($displayIds as $displayId) {
$customerGroupIds[] = $displayId['customerGroupId'];
}
$customerGroupsResult = $this->customerGroupRepository->search(
(new Criteria($customerGroupIds)),
$event->getContext()
);
$customerGroupNames = [];
if($customerGroupsResult) {
foreach($customerGroupsResult as $customerGroup) {
$customerGroupNames[] = $customerGroup->getName();
}
$currentCustomerGroup = $event->getSalesChannelContext()->getCurrentCustomerGroup()->getName();
if(!in_array($currentCustomerGroup, $customerGroupNames)) {
dump($currentCustomerGroup);
dump($customerGroupNames);
$redirectResponse = new RedirectResponse($this->router->generate('frontend.account.oauth.redirect', [
'redirect_to' => $event->getRequest()->getUri(),
'auto_group' => $customerGroupNames
]));
$redirectResponse->send();
exit;
}
}
}
}
}