src/Services/Cart.php line 486

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  4. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  5. use Symfony\Component\Mime\Address;
  6. use Symfony\Component\Mime\Email;
  7. /**
  8.  * Cart management, include sending emails for both customer et provider
  9.  */
  10. class Cart
  11. {
  12.     private $session;
  13.     private $cartContent = [];
  14.     private $cartTotalQuantity;
  15.     private $maxQuantityAllowed 100;
  16.     private $weightPrice false;
  17.     public function __construct($session)
  18.     {
  19.         $this->session $session;
  20.     }
  21.     /**
  22.      * Add product by its id to the cart session.
  23.      * Add the correct product quantity if this function
  24.      * is called again.
  25.      * 
  26.      * Example of $cartContent added to the session:
  27.      * $cartContent = [
  28.      *  $id => ["quantity" => 3],
  29.      *         ["product-custom-data" => ["message" => "Message preso", "police" => 1, "image" => 1]],
  30.      *  $id => ["quantity" => 1]
  31.      * ]
  32.      *
  33.      * @param [type] $id
  34.      * @return void
  35.      */
  36.     public function addToCart($id$quantity$customerCustomMessage$customerCustomPolice$customerCustomImage)
  37.     {
  38.         $this->cartContent = [];
  39.         //Récupérer les valeurs custom du client et procéder au vérifications de leurs intégrités via des methodes
  40.         //Ajouter ces valeurs à $this->cartContent[$id]
  41.         if ($this->cartSessionExists())
  42.         {
  43.             $this->cartContent $this->getCart();
  44.             if (isset($this->cartContent[$id]) && $quantity == 1)
  45.             {
  46.                 $this->cartContent[$id]['quantity']++;
  47.             }
  48.             elseif(isset($this->cartContent[$id]) && $quantity && $quantity <= $this->maxQuantityAllowed)
  49.             {
  50.                 $this->cartContent[$id]['quantity'] += $quantity;
  51.             }
  52.             elseif($quantity != && $quantity <= $this->maxQuantityAllowed)
  53.             {
  54.                 //Ajouter le nouveau produit
  55.                 $this->cartContent[$id] = ['quantity' => $quantity];
  56.             }
  57.             //Ajout des éléments de personnalisations
  58.             if ($this->isCustomizableProduct($customerCustomMessage))
  59.             {
  60.                 if ($customerCustomMessage && $customerCustomPolice && $customerCustomImage)
  61.                 {
  62.                     $this->cartContent[$id]['product-custom-data'] = ['message' => $customerCustomMessage'police' => $customerCustomPolice'image' => $customerCustomImage];
  63.                 }
  64.             }
  65.         }
  66.         if (!$this->cartSessionExists())
  67.         {
  68.             $this->cartContent[$id] = ['quantity' => $quantity];
  69.             
  70.             //Ajout des éléments de personnalisations
  71.             if ($this->isCustomizableProduct($customerCustomMessage))
  72.             {
  73.                 if ($customerCustomMessage && $customerCustomPolice && $customerCustomImage)
  74.                 {
  75.                     $this->cartContent[$id]['product-custom-data'] = ['message' => $customerCustomMessage'police' => $customerCustomPolice'image' => $customerCustomImage];
  76.                 }
  77.             }
  78.         }
  79.         $this->setCartTotalQuantity($this->getCartContent());
  80.         $this->setCartContent($this->cartContent);
  81.     }
  82.     /**
  83.      * Check if is a products is customizable when checking
  84.      * $customerCustomMessage that is required when a product is recognized as a customizable
  85.      * product by twig (twig shows form input to select a custom message when a product customizable is detected when 
  86.      * reading product.customizable entity if exists)
  87.      *
  88.      * @return boolean
  89.      */
  90.     public function isCustomizableProduct($customerCustomMessage)
  91.     {
  92.         if ($customerCustomMessage)
  93.         {
  94.             return true;
  95.         }
  96.     }
  97.     public function getMaxQuantityAllowed()
  98.     {
  99.         return $this->maxQuantityAllowed;
  100.     }
  101.     public function getCartTotalQuantity(): int
  102.     {
  103.         $cartSession $this->getCart();
  104.         if($cartSession)
  105.         {
  106.             $totalQuantity 0;
  107.             
  108.             foreach ($cartSession as $product)
  109.             {
  110.                 $totalQuantity += $product['quantity'];
  111.             }
  112.     
  113.             $this->cartTotalQuantity =$totalQuantity;
  114.     
  115.         }
  116.         
  117.         return (int)$this->cartTotalQuantity;
  118.     }
  119.     /**
  120.      * Save cart Total Quantity in $this->cartTotalQuantity
  121.      * cartTotalQuantity is obtained from current session.
  122.      *
  123.      * @param array $cartContent cart content from current session.
  124.      * @return void
  125.      */
  126.     public function setCartTotalQuantity($cartContent)
  127.     {
  128.         $quantity 0;
  129.         foreach($cartContent as $value)
  130.         {
  131.             $quantity += $value['quantity'];
  132.         }
  133.         $this->cartTotalQuantity $quantity;
  134.     }
  135.     /**
  136.      * Return the cart session content
  137.      *
  138.      */
  139.     public function getCart()
  140.     {
  141.         if ($this->cartSessionExists())
  142.         {
  143.             return $this->session->get('cart');
  144.         }
  145.         return false;
  146.     }
  147.     /**
  148.      * Get cart content from attribute this->cartContent
  149.      * ONLY USE THIS METHOD WHEN setCartContent() methode WASE USE ONCE.
  150.      * OTHERWISE, USE getCart() INSTEAD
  151.      * (See setCartContent method)
  152.      *
  153.      * @return void
  154.      */
  155.     public function getCartContent()
  156.     {
  157.         if (!empty($this->cartContent))
  158.         {
  159.             return $this->cartContent;
  160.         }
  161.         return 'The cart is empty';
  162.     }
  163.     /**
  164.      * Get cart products object alongsides products quantity for 
  165.      * twig display.
  166.      *
  167.      * @param [productRepository object] $productRepository
  168.      * @return void
  169.      */
  170.     public function getCartProducts($productRepository)
  171.     {
  172.         $cartContent $this->getCart();
  173.         $cartProducts = [];
  174.         if($this->cartSessionExists())
  175.         {
  176.             foreach ($cartContent as  $id => $productData)
  177.             {
  178.                 $cartProducts[] = ['product' => $productRepository->find($id), 'quantity' =>  $productData['quantity'], 'product_custom_data' => $productData['product-custom-data'] ?? null];
  179.             }
  180.         }
  181.         //dd($cartProducts);
  182.         return $cartProducts;
  183.     }
  184.     public function getTotalPrice($productRepository)
  185.     {
  186.         $cartProducts $this->getCart();
  187.         $products = [];
  188.         $totalPrice 0;
  189.         if($this->cartSessionExists())
  190.         {
  191.             foreach ($cartProducts as $id => $product)
  192.             {
  193.                 $productPrice $productRepository->find($id);
  194.                 $productPrice floatval($productPrice->getPrice());
  195.     
  196.                 $totalPrice += $product["quantity"] * $productPrice;
  197.                   
  198.             }
  199.         }
  200.         return $totalPrice;
  201.     }
  202.     /**
  203.      * Get total weight for displaying via twig / html for user view
  204.      *
  205.      * @param [type] $productRepo
  206.      * @return void
  207.      */
  208.     public function getTotalWeightForDisplay($productRepo)
  209.     {
  210.         $weightToDisplay $this->getTotalWeight($productRepo);
  211.         if (!is_string($weightToDisplay))
  212.         {
  213.             if ($weightToDisplay >= 1000)
  214.             {
  215.                 $weightToDisplay $weightToDisplay 1000;
  216.                 //$weightToDisplay = number_format($weightToDisplay, 3);
  217.                 $weightToDisplay $weightToDisplay.' Kg';
  218.                 return $weightToDisplay;
  219.     
  220.             }
  221.     
  222.             if ($weightToDisplay 1000)
  223.             {
  224.                 $weightToDisplay $weightToDisplay.' g';
  225.                 return $weightToDisplay;
  226.             }
  227.             
  228.         }
  229.         return $weightToDisplay;
  230.     }
  231.     /**
  232.      * Get ttcPrice AND Set weightPrice
  233.      *
  234.      * @param [ProductRepository] $productRepo
  235.      * @return void
  236.      */
  237.     public function getTtcPrice($productRepo)
  238.     {
  239.         $totalPrice $this->getTotalPrice($productRepo);
  240.         $totalWeight $this->getTotalWeight($productRepo);
  241.         $weightPrice 0;
  242.         $ttcPrice 0;
  243.         
  244.         switch ($totalWeight)
  245.         {
  246.             case $totalWeight >= && $totalWeight <= 19;
  247.                 $weightPrice 3;
  248.                 break;
  249.             case $totalWeight >= 20 && $totalWeight <= 99;
  250.                 $weightPrice 4.80;
  251.                 break;
  252.             case $totalWeight >= 100 && $totalWeight <= 249;
  253.                 $weightPrice 6.80;
  254.                 break;
  255.             case $totalWeight >= 250 && $totalWeight <= 499;
  256.                 $weightPrice 8.20;
  257.                 break;
  258.             case $totalWeight >= 500 && $totalWeight <= 999;
  259.                 $weightPrice 9.80;
  260.                 break;
  261.             case $totalWeight >= 1000 && $totalWeight <= 1999;
  262.                 $weightPrice 15.50;
  263.                 break;
  264.             case $totalWeight >= 2000;
  265.                 $weightPrice 20.50;
  266.                 break;
  267.         }
  268.         
  269.         $this->weightPrice $weightPrice;
  270.         $ttcPrice $totalPrice += $weightPrice;
  271.         return $ttcPrice;
  272.     }
  273.     /**
  274.      * MUST BE USED AFTER GET TTCPRICE.
  275.      * Beacause getTtcPrice() is the setter for weightPrice
  276.      *
  277.      * @return float
  278.      */
  279.     public function getWeightPrice()
  280.     {
  281.         return $this->weightPrice;
  282.     }
  283.     /**
  284.      * Get total weight in Grammes from all products
  285.      *
  286.      * @param [ProductRepository] $productRepo
  287.      * @return void
  288.      */
  289.     public function getTotalWeight($productRepo)
  290.     {
  291.         if (!$this->cartSessionExists())
  292.         {
  293.             return 'Le panier est vide';
  294.         }
  295.         $cartProducts $this->getCartProducts($productRepo);
  296.         $totalWeight 0;
  297.         
  298.         foreach ($cartProducts as $product)
  299.         {
  300.             $productWeightUnit $product['product']->getWeightUnit();
  301.             if ($productWeightUnit == 'kg')
  302.             {
  303.                 $productWeightInGramme $product['product']->getWeight() * 1000;
  304.                 $totalWeight += $productWeightInGramme  $product['quantity'];
  305.             }
  306.             if ($productWeightUnit == 'g')
  307.             {
  308.                 $productWeight $product['product']->getWeight();
  309.                 $totalWeight += $productWeight $product['quantity'];
  310.             }
  311.           
  312.         }
  313.         return $totalWeight;
  314.     }
  315.     /**
  316.      * Save cartContent to the cart session
  317.      *
  318.      * @param array $cartContent
  319.      * @return void
  320.      */
  321.     public function setCartContent(array $cartContent)
  322.     {
  323.         $this->session->set('cart'$cartContent);
  324.     }
  325.     /**
  326.      * Check if the cart attribut exist inside the 
  327.      * current session.
  328.      *
  329.      * @return boolean
  330.      */
  331.     public function cartSessionExists()
  332.     {
  333.         if ($this->session->get('cart'))
  334.         {
  335.             return true;
  336.         }
  337.     }
  338.     /**
  339.      * Clear cart content session
  340.      *
  341.      * @return void
  342.      */
  343.     public function clearCart()
  344.     {
  345.         $this->session->remove('cart');
  346.     }
  347.     /**
  348.      * Send an email with billing summary for the custommer and to the provider at the end of the transaction 
  349.      * Flush cart session at the end.
  350.      *
  351.      * @param [type] $user
  352.      * @param [type] $mailer
  353.      * @param [type] $billingDetails
  354.      * @param [type] $productRepo
  355.      * @return void
  356.      */
  357.     public function sendEndTransactionMail($user$mailer$billingDetails$productRepo)
  358.     {
  359.         $cartProducts $this->getCartProducts($productRepo);
  360.         $totalPrice $this->getTotalPrice($productRepo);
  361.         $this->getTtcPrice($productRepo); //Must be used to set weightPrice Cart object's attribute to get getWeightPrice used, a default from my part ...
  362.         $totalWeight $this->getTotalWeightForDisplay($productRepo);
  363.         $weightPrice $this->getWeightPrice();
  364.         $transactionId $billingDetails['captureResponse']['jsonResponse']['purchase_units'][0]['payments']['captures'][0]['id'];
  365.         $selectedUserEmail $billingDetails["email"];
  366.         //TO CUSTOMER MAIL
  367.         $customerMail = (new TemplatedEmail())
  368.         ->from('no-reply@lestresorsdeluna.fr')// METTRE LE BON MAIL correspondant à celui d'ovh (A faire corresspodre avec le .env) (celui des trésors de luna)
  369.         ->to($selectedUserEmail)
  370.         ->subject('Les trésors de Luna. Récapitulatif d\'achat')
  371.         ->context([
  372.             'surname' => $user->getSurname(),
  373.             'firstname' => $user->getFirstname(),
  374.             'cartProducts' => $cartProducts,
  375.             'totalPrice' => $totalPrice,
  376.             'totalWeight' => $totalWeight,
  377.             'weightPrice' => $weightPrice,
  378.             'transactionId' => $transactionId
  379.         ])
  380.         ->htmlTemplate('emails/buyingSummary.html.twig');
  381.         //TO PROVIDER MAIL
  382.         $providerMail = (new TemplatedEmail())
  383.         ->from('no-reply@lestresorsdeluna.fr')
  384.         ->to(new Address('contact.tresorsdeluna@gmail.com'))// METTRE LE BON MAIL (celui des trésors de luna gmail: contact.tresorsdeluna@gmail.com)
  385.         ->subject("Demande d'achat")
  386.         ->context([
  387.             'surname' => $billingDetails["lastname"],
  388.             'firstname' => $billingDetails["firstname"],
  389.             'mail' => $selectedUserEmail,
  390.             'phone' => !empty($billingDetails["phone"]) ? $billingDetails["phone"] : null ,
  391.             'city' => $billingDetails["city"],
  392.             'zip' => $billingDetails["zip"],
  393.             'adress' => $billingDetails["address"],
  394.             'additionalAdressInfo' => !empty($billingDetails["additional-address-information"]) ? $billingDetails["additional-address-information"] : null,
  395.             'additionalInformation' => !empty($billingDetails["additionalInformation"]) ? $billingDetails["additionalInformation"] : null,
  396.             'cartProducts' => $cartProducts,
  397.             'totalPrice' => $totalPrice,
  398.             'totalWeight' => $totalWeight,
  399.             'weightPrice' => $weightPrice,
  400.             'transactionId' => $transactionId
  401.         ])
  402.         ->htmlTemplate('emails/buying_order.html.twig');
  403.         try
  404.         {
  405.             $mailer->send($customerMail);
  406.             $mailer->send($providerMail);
  407.             return true;
  408.         }
  409.         catch(TransportExceptionInterface $e)
  410.         {
  411.             return false;
  412.         }
  413.         
  414.     }
  415.     /**
  416.      * Method for testing purpose.
  417.      *
  418.      * @return void
  419.      */
  420.     public function test()
  421.     {
  422.         $session $this->session->get('cart');
  423.         return $session;
  424.     }
  425. }
  426. ?>