<?php
namespace App\EventSubscriber\Cart;
use App\Model\Customer;
use Exception;
use Factory\CartBundle\Classes\CartEvents;
use Factory\CartBundle\Event\CartEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Factory\CartBundle\Repository\CartRepository;
use Pimcore\Bundle\EcommerceFrameworkBundle\EnvironmentInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Request;
class CartActionSubscriber implements EventSubscriberInterface
{
/**
* @var Security
*/
protected $security;
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var CartRepository
*/
protected $cartRepository;
/**
* @var EnvironmentInterface
*/
protected $environment;
public static function getSubscribedEvents()
{
return [
CartEvents::CART_BEFORE_BULK_ADD => [
['prepareCartForDivision', 10],
['checkProductsCompatibility', 8],
['updateCustomerAddress', 5]
],
CartEvents::CART_BEFORE_FETCH => [
['prepareCartNameForDivision', 10],
['updateCustomerAddress', 5]
],
CartEvents::CART_BEFORE_ADD => [
['prepareCartForDivision', 10],
['checkProductCompatibility', 8],
['checkAvailability', 6],
['updateCustomerAddress', 5]
],
CartEvents::CART_BEFORE_UPDATE => [
['prepareCartForDivision', 10],
['checkProductsCompatibility', 8],
['checkAvailability', 6],
['updateCustomerAddress', 5]
],
CartEvents::CART_BEFORE_REMOVE => [
['prepareCartForDivision', 10],
['updateCustomerAddress', 5]
],
CartEvents::CART_BEFORE_COUPON_CODE_ADD => [
['prepareCartForDivision', 10]
],
CartEvents::CART_BEFORE_COUPON_CODE_REMOVE => [
['prepareCartForDivision', 10]
]
];
}
public function __construct(
Security $security,
RequestStack $requestStack,
CartRepository $cartRepository,
EnvironmentInterface $environment
) {
$this->security = $security;
$this->requestStack = $requestStack;
$this->cartRepository = $cartRepository;
$this->environment = $environment;
}
protected function getRequest(): Request
{
return $this->requestStack->getMasterRequest();
}
public function updateCustomerAddress()
{
$env = Factory::getInstance()->getEnvironment();
if ($env->getCurrentUserId() < 1) {
$env->setBillingAddress(null);
return;
}
$customer = Customer::getById($env->getCurrentUserId());
$billing_address = null;
$shipping_address = null;
$billing_default_address = $customer->getDefaultBillingAddressObject();
$shipping_default_address = $customer->getDefaultShippingAddressObject();
if ($this->getRequest()->cookies->has('__billing_address_id')) {
$billing_address_id = $this->getRequest()->cookies->get('__billing_address_id');
$billing_address = $customer->getAddress($billing_address_id);
}
if ($this->getRequest()->cookies->has('__shipping_address_id')) {
$shipping_address_id = $this->getRequest()->cookies->get('__shipping_address_id');
$shipping_address = $customer->getAddress($shipping_address_id);
}
if (!$billing_address && !$billing_default_address) {
throw new Exception('User must have selected default billing address!');
}
$env->setBillingAddress($billing_address ?? $billing_default_address);
$env->setShippingAddress($shipping_address ?? $shipping_default_address);
$env->save();
}
public function prepareCartNameForDivision(CartEvent $event)
{
if (!$this->hasDivisionSegment()) {
return;
}
$cartName = $this->createCartName($this->getDivisionSegment());
$event->setArgument('cartName', $cartName);
}
public function prepareCartForDivision(CartEvent $event)
{
if (!$this->hasDivisionSegment()) {
return;
}
$cartName = $this->createCartName($this->getDivisionSegment());
$cart = $this->getCart($cartName, $this->getUser());
$event->setSubject($cart);
}
public function checkProductsCompatibility(CartEvent $event)
{
//dd($this->requestStack->getCurrentRequest()->get('products'));
if (!$this->hasDivisionSegment()) {
return;
}
// Do Nothing
}
public function checkProductCompatibility(CartEvent $event)
{
if (!$this->hasDivisionSegment()) {
return;
}
// Do Nothing
}
public function checkAvailability(CartEvent $event)
{
if (!$this->hasDivisionSegment()) {
return;
}
// Do Nothing
}
protected function createCartName($division)
{
return $division . '_cart';
}
protected function getCart($cartName, $user = null)
{
if ($user) {
return $this->cartRepository->getDatabaseCart($user, $cartName) ?? $this->cartRepository->createDatabaseCart($user, $cartName);
}
return $this->cartRepository->getSessionCart($cartName) ?? $this->cartRepository->createSessionCart($cartName);
}
protected function getUser()
{
if (!$token = $this->security->getToken()) {
return null;
}
$user = $this->security->getToken()->getUser();
if (!is_object($user)) {
// eg. "anon."
return null;
}
return $user;
}
protected function hasDivisionSegment()
{
return !empty($this->getDivisionSegment());
}
protected function getDivisionSegment()
{
return $this->getRequest()->get('division');
}
}