<?php declare(strict_types=1);
namespace Samson\Subscriber;
/***
*
* This file is part of the "SAMSON Shop" project.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2024
*
***/
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AssetManagementCustomerSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $customerRepository;
public function __construct(EntityRepositoryInterface $customerRepository)
{
$this->customerRepository = $customerRepository;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [CustomerEvents::CUSTOMER_LOADED_EVENT => 'addCorporation'];
}
public function addCorporation(EntityLoadedEvent $event)
{
if (empty($event->getEntities())) {
return;
}
$customer = $event->getEntities()[0];
if (is_null($customer->getExtension("corporations"))) {
$criteria = new Criteria([$customer->getId()]);
$criteria->addAssociation('corporations');
/** @var CustomerEntity $customerEntity */
$customerEntity = $this->customerRepository->search($criteria, $event->getContext())->first();
$corporations = $customerEntity->getExtension('corporations');
$customer->addExtension('corporations', $corporations);
}
}
}