<?php
namespace App\Utils\Uniqskills\Cart\Actions;
use App\Entity\Gos\PortalSettings;
use App\Model\Cart;
use App\Utils\Api\ApiCartService;
use App\Utils\OrderServices;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\User\UserInterface;
class CartDetailsService
{
private $request;
private $apiCartService;
private $cartHash;
private $em;
private $cartV4Logger;
public function __construct(
EntityManagerInterface $em,
RequestStack $requestStack,
ApiCartService $apiCartService,
LoggerInterface $cartV4Logger
) {
$this->apiCartService = $apiCartService;
$this->request = $requestStack->getCurrentRequest();
$this->cartHash = $this->request->getSession()->get('cartHash');
$this->em = $em;
$this->cartV4Logger = $cartV4Logger;
}
public function getCart(?UserInterface $user): array
{
$cart = $this->apiCartService->getNewCart($user, $this->cartHash);
if ($this->validateCartStructure($cart))
{
if(!$this->request->getSession()->has('cartHash'))
{
$this->request->getSession()->set('cartHash', $cart['hash']);
}
return $cart;
}
return Cart::NEW_CART_MODEL;
}
public function getPositionsForPortal(PortalSettings $portalSettings): array
{
$positions = [];
foreach ($portalSettings->getPositions() as $position) {
$positions[$position->getName()] = $position->getName();
}
return $positions;
}
public function getHonorificsForPortal(PortalSettings $portalSettings): array
{
$honorifics = [];
foreach ($portalSettings->getHonorifics() as $honorific) {
$honorifics[$honorific->getName()] = $honorific->getName();
}
return $honorifics;
}
private function validateCartStructure(array $cart): bool
{
foreach (array_keys(Cart::NEW_CART_MODEL) as $key)
{
if (array_key_exists($key, $cart))
{
continue;
}
$this->cartV4Logger->notice('Cart structure validation failed', [
'missingKey' => $key,
'cart' => $cart,
'cartHash' => $this->cartHash
]);
return false;
}
return true;
}
}