| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Sylius\ShopApiPlugin\Handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Sylius\Component\Core\Model\AddressInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Sylius\Component\Resource\Factory\FactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Sylius\Component\Resource\Repository\RepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Sylius\ShopApiPlugin\Command\CreateAddress; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Webmozart\Assert\Assert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | final class CreateAddressHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var RepositoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $addressRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var FactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $addressFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var RepositoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $countryRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var RepositoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $provinceRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var TokenStorageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $tokenStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * CreateAddressHandler constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param RepositoryInterface $addressRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param RepositoryInterface $countryRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param RepositoryInterface $provinceRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param FactoryInterface $addressFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param TokenStorageInterface $tokenStorage | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 50 |  | View Code Duplication |     public function __construct( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         RepositoryInterface $addressRepository, | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         RepositoryInterface $countryRepository, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         RepositoryInterface $provinceRepository, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         FactoryInterface $addressFactory, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         TokenStorageInterface $tokenStorage | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $this->addressRepository = $addressRepository; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $this->countryRepository = $countryRepository; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->provinceRepository = $provinceRepository; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $this->addressFactory = $addressFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $this->tokenStorage = $tokenStorage; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function handle(CreateAddress $command): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $user = $this->tokenStorage->getToken()->getUser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $customer = $user->getCustomer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->assertCountryExists($command->countryCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         /** @var AddressInterface $address */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $address = $this->addressFactory->createNew(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $address->setFirstName($command->firstName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $address->setLastName($command->lastName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $address->setCompany($command->company()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $address->setStreet($command->street()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $address->setCountryCode($command->countryCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $address->setCity($command->city()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $address->setPostcode($command->postcode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $address->setPhoneNumber($command->phoneNumber()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if (null !== $command->provinceCode()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $province = $this->provinceRepository->findOneBy(['code' => $command->provinceCode()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->assertProvinceExists($province); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $address->setProvinceCode($province->getCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $address->setProvinceName($province->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $customer->addAddress($address); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->addressRepository->add($address); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param string $countryCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     private function assertCountryExists(string $countryCode): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         Assert::notNull($this->countryRepository->findOneBy(['code' => $countryCode]), 'Country does not exist.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param $province | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     private function assertProvinceExists($province): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         Assert::notNull($province, 'Province does not exist.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.