| 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 |  |  | namespace Sylius\Behat\Context\Setup; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Behat\Behat\Context\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Doctrine\Common\Persistence\ObjectManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Sylius\Component\Core\Factory\RuleFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Sylius\Component\Core\Model\PromotionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Sylius\Component\Core\Test\Factory\TestPromotionFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sylius\Component\Core\Test\Services\SharedStorageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Sylius\Component\Promotion\Factory\ActionFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Sylius\Component\Promotion\Repository\PromotionRepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @author Mateusz Zalewski <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | final class PromotionContext implements Context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var SharedStorageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $sharedStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var ActionFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private $actionFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var RuleFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private $ruleFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var TestPromotionFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     private $testPromotionFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @var PromotionRepositoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     private $promotionRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @var ObjectManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     private $objectManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param SharedStorageInterface $sharedStorage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param ActionFactoryInterface $actionFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param RuleFactoryInterface $ruleFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param TestPromotionFactoryInterface $testPromotionFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param PromotionRepositoryInterface $promotionRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param ObjectManager $objectManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         SharedStorageInterface $sharedStorage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         ActionFactoryInterface $actionFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         RuleFactoryInterface $ruleFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         TestPromotionFactoryInterface $testPromotionFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         PromotionRepositoryInterface $promotionRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ObjectManager $objectManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->sharedStorage = $sharedStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->actionFactory = $actionFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->ruleFactory = $ruleFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->testPromotionFactory = $testPromotionFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->promotionRepository = $promotionRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $this->objectManager = $objectManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @Given there is a promotion :promotionName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function thereIsPromotion($promotionName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $promotion = $this->testPromotionFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ->createForChannel($promotionName, $this->sharedStorage->get('channel')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $this->promotionRepository->add($promotion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->sharedStorage->set('promotion', $promotion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @Given /^([^"]+) gives ("[^"]+") fixed discount to every order$/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     public function itGivesFixedDiscountToEveryOrder(PromotionInterface $promotion, $amount) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $action = $this->actionFactory->createFixedDiscount($amount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $promotion->addAction($action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $this->objectManager->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @Given /^([^"]+) gives ("[^"]+") percentage discount to every order$/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     public function itGivesPercentageDiscountToEveryOrder(PromotionInterface $promotion, $discount) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $action = $this->actionFactory->createPercentageDiscount($discount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $promotion->addAction($action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->objectManager->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @Given /^([^"]+) gives ("[^"]+") fixed discount to every order with quantity at least ([^"]+)$/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast(PromotionInterface $promotion, $amount, $quantity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $action = $this->actionFactory->createFixedDiscount($amount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $promotion->addAction($action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $rule = $this->ruleFactory->createCartQuantity((int) $quantity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $promotion->addRule($rule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $this->objectManager->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @Given /^([^"]+) gives ("[^"]+") fixed discount to every order with items total at least ("[^"]+")$/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     public function itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         PromotionInterface $promotion, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $amount, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $targetAmount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $action = $this->actionFactory->createFixedDiscount($amount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $promotion->addAction($action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $rule = $this->ruleFactory->createItemTotal($targetAmount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $promotion->addRule($rule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $this->objectManager->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @Given /^([^"]+) gives ("[^"]+") percentage discount on shipping to every order$/ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |     public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount) | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |         $action = $this->actionFactory->createPercentageShippingDiscount($discount); | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         $promotion->addAction($action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->objectManager->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 158 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |  |