<?php declare(strict_types=1);
namespace BaumarktTheme\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Uuid\Uuid;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* @property RequestStack $requestStack
*/
class CustomerSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $ianeoCustomerAttributesEntity;
/**
* @throws \Exception
*/
public function __construct(
RequestStack $requestStack,
EntityRepositoryInterface $ianeoCustomerAttributesRepository
)
{
$this->requestStack = $requestStack;
$this->ianeoCustomerAttributesEntity = $ianeoCustomerAttributesRepository;
}
public static function getSubscribedEvents(): array
{
return [
'customer.written' => 'onCustomerWritten',
'customer_address.written' => 'onCustomerAddressWritten'
];
}
public function onCustomerWritten(EntityWrittenEvent $event)
{
$customerId = $event->getIds()[0];
// Return when event not triggered in frontend context with httpRequest
if(empty($this->requestStack->getCurrentRequest())) {
return;
}
$personalPrimaryPhone = $this->requestStack->getCurrentRequest()->get('personalPrimaryPhone');
if (!$personalPrimaryPhone) {
return;
}
$this->upsertCustomerAttributes($customerId, $personalPrimaryPhone, $event->getContext());
}
public function onCustomerAddressWritten(EntityWrittenEvent $event) : void
{
$customer = $event->getPayloads();
if (empty($customer) || empty($customer[0]['customerId'])) {
return;
}
$customerId = $customer[0]['customerId'];
// Return when event not triggered in frontend context with httpRequest
if(
empty($this->requestStack->getCurrentRequest())
|| empty($this->requestStack->getCurrentRequest()->request->get('address'))
|| empty($this->requestStack->getCurrentRequest()->request->get('address')['personalPrimaryPhone'])
) {
return;
}
if($this->requestStack->getCurrentRequest()->request->get('address') !== null) {
$personalPrimaryPhone = $this->requestStack->getCurrentRequest()->request->get('address')['personalPrimaryPhone'];
} else {
$personalPrimaryPhone = $this->requestStack->getCurrentRequest()->request->get('personalPrimaryPhone');
}
if (!$personalPrimaryPhone) {
return;
}
$this->upsertCustomerAttributes($customerId, $personalPrimaryPhone, $event->getContext());
}
private function upsertCustomerAttributes($customerId, $personalPrimaryPhone, $context)
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('customerId', $customerId));
$ianeoCustomerAttributesEntity = $this->ianeoCustomerAttributesEntity->search(
$criteria,
$context
)->first();
$attributesId = Uuid::randomHex();
if ($ianeoCustomerAttributesEntity) {
$attributesId = $ianeoCustomerAttributesEntity->getId();
}
$this->ianeoCustomerAttributesEntity->upsert([
[
'id' => $attributesId,
'customerId' => $customerId,
'primaryPhoneNumber' => $personalPrimaryPhone
]
], $context);
}
}