| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Sylius package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Paweł Jędrzejewski | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Sylius\Component\Core\Cart\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Sylius\Component\Channel\Context\ChannelNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Sylius\Component\Core\Context\ShopperContextInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Sylius\Component\Core\Model\ChannelInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sylius\Component\Core\Model\CustomerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Sylius\Component\Core\Model\OrderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Sylius\Component\Currency\Context\CurrencyNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Sylius\Component\Locale\Context\LocaleNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Sylius\Component\Order\Context\CartContextInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Sylius\Component\Order\Context\CartNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Webmozart\Assert\Assert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | final class ShopBasedCartContext implements CartContextInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var CartContextInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $cartContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var ShopperContextInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $shopperContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var OrderInterface|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     private $cart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param CartContextInterface $cartContext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param ShopperContextInterface $shopperContext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function __construct(CartContextInterface $cartContext, ShopperContextInterface $shopperContext) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->cartContext = $cartContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->shopperContext = $shopperContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function getCart(): BaseOrderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if (null !== $this->cart) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             return $this->cart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         /** @var OrderInterface $cart */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $cart = $this->cartContext->getCart(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         Assert::isInstanceOf($cart, OrderInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             /** @var ChannelInterface $channel */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $channel = $this->shopperContext->getChannel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $cart->setChannel($channel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $cart->setCurrencyCode($channel->getBaseCurrency()->getCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $cart->setLocaleCode($this->shopperContext->getLocaleCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } catch (ChannelNotFoundException | CurrencyNotFoundException | LocaleNotFoundException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             throw new CartNotFoundException('Sylius was not able to prepare the cart.', $exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         /** @var CustomerInterface $customer */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $customer = $this->shopperContext->getCustomer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if (null !== $customer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $this->setCustomerAndAddressOnCart($cart, $customer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->cart = $cart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return $cart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param OrderInterface $cart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param CustomerInterface $customer | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |     private function setCustomerAndAddressOnCart(OrderInterface $cart, CustomerInterface $customer): void | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         $cart->setCustomer($customer); | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         $defaultAddress = $customer->getDefaultAddress(); | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         if (null !== $defaultAddress) { | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |             $clonedAddress = clone $defaultAddress; | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |             $clonedAddress->setCustomer(null); | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |             $cart->setShippingAddress($clonedAddress); | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |