<?php declare(strict_types=1);
namespace VRPayment\VRPay\Subscriber;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
use VRPayment\VRPay\Core\Checkout\Customer\CustomerRegistrationsExtension;
/**
* Class AccountSubscriber
* When the account payments page was loaded, we inject our saved registrations for the current customer into the page
*
* @package VRPayment\VRPay\Subscriber
*/
class AccountSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService $systemConfigService
*/
private $systemConfigService;
/**
* @var EntityRepositoryInterface
*/
private $registrationsRepository;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
AccountPaymentMethodPageLoadedEvent::class => 'onAccountPaymentPageLoaded'
];
}
public function __construct(SystemConfigService $systemConfigService, EntityRepositoryInterface $registrationsRepository)
{
$this->systemConfigService = $systemConfigService;
$this->registrationsRepository = $registrationsRepository;
}
/**
* called when the account payments page was loaded.
* Reads all registrations from the db and displays it to the customer
*
* @param AccountPaymentMethodPageLoadedEvent $event
*/
public function onAccountPaymentPageLoaded(AccountPaymentMethodPageLoadedEvent $event)
{
$enableRegistrations = $this->systemConfigService->get(
'VRPay.config.saveRegistrations',
$event->getSalesChannelContext()->getSalesChannel()->getId()
);
$enableRegistrationInteraction = $this->systemConfigService->get(
'VRPay.config.allowInteractRegistrations',
$event->getSalesChannelContext()->getSalesChannel()->getId()
);
if(!$enableRegistrations || !$enableRegistrationInteraction)
{
return;
}
$customer = $event->getSalesChannelContext()->getCustomer();
if($customer->getGuest())
{
// registrations are always disabled for guests
return;
}
$extension = new CustomerRegistrationsExtension();
$extension->setRegistrations(
$this->loadCustomerRegistrations($customer, $event->getSalesChannelContext())->getElements()
);
$page = $event->getPage();
$page->addExtension('vrpaymentRegistrations', $extension);
}
/*
* internals
*/
/**
* Loads all registrations that are currently stored for the given customer
*
* @param CustomerEntity $customer
* @param SalesChannelContext $salesChannelContext
*
* @return EntityCollection
*/
private function loadCustomerRegistrations(CustomerEntity $customer, SalesChannelContext $salesChannelContext): EntityCollection
{
return $this->registrationsRepository->search(
(new Criteria())
->addFilter(new EqualsFilter('customerId', $customer->getId()))
->setLimit(100),
$salesChannelContext->getContext()
)->getEntities();
}
}