| 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\Addressing\Model\ZoneInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Sylius\Component\Core\Model\ShippingMethodInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Sylius\Component\Core\Test\Services\SharedStorageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sylius\Component\Resource\Factory\FactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Sylius\Component\Resource\Repository\RepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Sylius\Component\Shipping\Calculator\DefaultCalculators; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Sylius\Component\Taxation\Model\TaxCategoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @author Arkadiusz Krakowiak <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | final class ShippingContext implements Context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var RepositoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $shippingMethodRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @var RepositoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private $zoneRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @var FactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private $shippingMethodFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @var ObjectManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     private $shippingMethodManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @var SharedStorageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     private $sharedStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param RepositoryInterface $shippingMethodRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param RepositoryInterface $zoneRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param FactoryInterface $shippingMethodFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param ObjectManager $shippingMethodManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param SharedStorageInterface $sharedStorage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         RepositoryInterface $shippingMethodRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         RepositoryInterface $zoneRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         FactoryInterface $shippingMethodFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ObjectManager $shippingMethodManager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         SharedStorageInterface $sharedStorage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->shippingMethodRepository = $shippingMethodRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->zoneRepository = $zoneRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->shippingMethodFactory = $shippingMethodFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->shippingMethodManager = $shippingMethodManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->sharedStorage = $sharedStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @Transform :shippingMethodName shipping method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @Transform shipping method :shippingMethodName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function getShippingMethodByName($shippingMethodName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $shippingMethod = $this->shippingMethodRepository->findOneBy(['name' => $shippingMethodName]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if (null === $shippingMethod) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             throw new \Exception('Shipping method with name "'.$shippingMethodName.'" does not exist'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return $shippingMethod; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @Given the store ships everything for free within :zone zone | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @Given /^the store ships everything for free for (the rest of the world)$/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     public function storeShipsEverythingForFree(ZoneInterface $zone = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $this->createShippingMethod('Free', $zone); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @Given /^the store ships everywhere for free$/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public function theStoreShipsEverywhereForFree() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         foreach ($this->zoneRepository->findAll() as $zone) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $this->createShippingMethod('Free', $zone); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @Given /^the store has "([^"]*)" shipping method with ("[^"]+") fee$/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @Given /^the store has "([^"]*)" shipping method with ("[^"]+") fee within ("([^"]*)" zone)$/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @Given /^the store has "([^"]*)" shipping method with ("[^"]+") fee for (the rest of the world)$/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     public function storeHasShippingMethodWithFee($shippingMethodName, $fee, ZoneInterface $zone = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->createShippingMethod($shippingMethodName, $zone, 'en', ['amount' => $fee]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @Given /^(shipping method "[^"]+") belongs to ("[^"]+" tax category)$/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function shippingMethodBelongsToTaxCategory(ShippingMethodInterface $shippingMethod, TaxCategoryInterface $taxCategory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $shippingMethod->setTaxCategory($taxCategory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $this->shippingMethodManager->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param string $locale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param array $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param string $calculator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param ZoneInterface|null $zone | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     private function createShippingMethod( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $zone = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $locale = 'en', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $configuration = ['amount' => 0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $calculator = DefaultCalculators::FLAT_RATE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         if (null === $zone) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             $zone = $this->sharedStorage->get('zone'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $shippingMethod = $this->shippingMethodFactory->createNew(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $shippingMethod->setCode($this->generateCodeFromNameAndZone($name, $zone->getCode())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $shippingMethod->setName($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $shippingMethod->setCurrentLocale($locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $shippingMethod->setConfiguration($configuration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $shippingMethod->setCalculator($calculator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $shippingMethod->setZone($zone); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $this->shippingMethodRepository->add($shippingMethod); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @param string $shippingMethodName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @param string|null $zoneCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     private function generateCodeFromNameAndZone($shippingMethodName, $zoneCode = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         return str_replace([' ', '-'], '_', strtolower($shippingMethodName)).'_'.strtolower($zoneCode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  |