| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace solutionDrive\SyliusProductBundlesPlugin\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use solutionDrive\SyliusProductBundlesPlugin\Entity\ProductBundleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use solutionDrive\SyliusProductBundlesPlugin\Entity\ProductBundleSlotInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use solutionDrive\SyliusProductBundlesPlugin\Service\Options\ProductBundleSlotOptionsInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Sylius\Component\Core\Model\ProductInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Sylius\Component\Resource\Factory\FactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Created by solutionDrive GmbH | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @copyright 2018 solutionDrive GmbH | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class ProductBundleCreator implements ProductBundleCreatorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var FactoryInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $productBundleFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var FactoryInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $productBundleSlotFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var ProductBundleInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $productBundle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         FactoryInterface $productBundleFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         FactoryInterface $productBundleSlotFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->productBundleFactory = $productBundleFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->productBundleSlotFactory = $productBundleSlotFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function createProductBundle(string $productBundleName, ProductInterface $productBundleProduct): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->productBundle = $this->productBundleFactory->createNew(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->productBundle->setName($productBundleName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->productBundle->setProduct($productBundleProduct); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function getProductBundle(): ProductBundleInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         return $this->productBundle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function addSlot(string $slotName, ?ProductBundleSlotOptionsInterface $options = null, array $products = []): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         /** @var ProductBundleSlotInterface $slot */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $slot = $this->createSlot($slotName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->applyOptionsToSlot($options, $slot); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->addProductsToSlot($products, $slot); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->addSlotToBundle($options, $slot); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     private function applyOptionsToSlot(?ProductBundleSlotOptionsInterface $options, ProductBundleSlotInterface $slot): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if (null !== $options && null !== $options->getPosition()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $slot->setPosition($options->getPosition()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     private function addProductsToSlot(array $products, ProductBundleSlotInterface $slot): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         foreach ($products as $product) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $this->addProductToSlot($product, $slot); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     private function addProductToSlot(ProductInterface $product, ProductBundleSlotInterface $slot): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $slot->addProduct($product); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     private function addSlotToBundle(?ProductBundleSlotOptionsInterface $options, $slot): void | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         if (null !== $options && $options->isPresentationSlot()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             $this->productBundle->setPresentationSlot($slot); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         $this->productBundle->addSlot($slot); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     private function createSlot(string $slotName): ProductBundleSlotInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         /** @var ProductBundleSlotInterface $slot */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $slot = $this->productBundleSlotFactory->createNew(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $slot->setName($slotName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $slot->setBundle($this->productBundle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         return $slot; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  |