src/Utils/Uniqskills/Cart/Actions/CartDetailsService.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Utils\Uniqskills\Cart\Actions;
  3. use App\Entity\Gos\PortalSettings;
  4. use App\Model\Cart;
  5. use App\Utils\Api\ApiCartService;
  6. use App\Utils\OrderServices;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. class CartDetailsService
  12. {
  13.     private $request;
  14.     private $apiCartService;
  15.     private $cartHash;
  16.     private $em;
  17.     private $cartV4Logger;
  18.     public function __construct(
  19.         EntityManagerInterface $em,
  20.         RequestStack $requestStack,
  21.         ApiCartService $apiCartService,
  22.         LoggerInterface $cartV4Logger
  23.     ) {
  24.         $this->apiCartService $apiCartService;
  25.         $this->request $requestStack->getCurrentRequest();
  26.         $this->cartHash $this->request->getSession()->get('cartHash');
  27.         $this->em $em;
  28.         $this->cartV4Logger $cartV4Logger;
  29.     }
  30.     public function getCart(?UserInterface $user): array
  31.     {
  32.         $cart $this->apiCartService->getNewCart($user$this->cartHash);
  33.         if ($this->validateCartStructure($cart))
  34.         {
  35.             if(!$this->request->getSession()->has('cartHash'))
  36.             {
  37.                 $this->request->getSession()->set('cartHash'$cart['hash']);
  38.             }
  39.             return $cart;
  40.         }
  41.         return Cart::NEW_CART_MODEL;
  42.     }
  43.     public function getPositionsForPortal(PortalSettings $portalSettings): array
  44.     {
  45.         $positions = [];
  46.         foreach ($portalSettings->getPositions() as $position) {
  47.             $positions[$position->getName()] = $position->getName();
  48.         }
  49.         return $positions;
  50.     }
  51.     public function getHonorificsForPortal(PortalSettings $portalSettings): array
  52.     {
  53.         $honorifics = [];
  54.         foreach ($portalSettings->getHonorifics() as $honorific) {
  55.             $honorifics[$honorific->getName()] = $honorific->getName();
  56.         }
  57.         return $honorifics;
  58.     }
  59.     private function validateCartStructure(array $cart): bool
  60.     {
  61.         foreach (array_keys(Cart::NEW_CART_MODEL) as $key)
  62.         {
  63.             if (array_key_exists($key$cart))
  64.             {
  65.                 continue;
  66.             }
  67.             $this->cartV4Logger->notice('Cart structure validation failed', [
  68.                 'missingKey' => $key,
  69.                 'cart'       => $cart,
  70.                 'cartHash'   => $this->cartHash
  71.             ]);
  72.             return false;
  73.         }
  74.         return true;
  75.     }
  76. }