| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace WeDevBr\Bankly\Validators\Pix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use WeDevBr\Bankly\Types\Pix\BankAccount; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use WeDevBr\Bankly\Types\Pix\PixCashoutStaticQrCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * PixCashoutValidator class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * PHP version 7.3|7.4|8.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author    WeDev Brasil Team <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author    Rafael Teixeira <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @copyright 2020 We Dev Tecnologia Ltda | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @link      https://github.com/wedevBr/bankly-laravel/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class PixCashoutStaticQrCodeValidator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var PixCashoutStaticQrCode */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $pixCashoutStaticQrCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param PixCashoutStaticQrCode $pixCashoutStaticQrCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function __construct(PixCashoutStaticQrCode $pixCashoutStaticQrCode) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->pixCashoutStaticQrCode = $pixCashoutStaticQrCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Validate the attributes of the PIX cashout class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function validate(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->validateAmount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->validateDescription(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->validateSender(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->validateRecipient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->validateInitializationType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->validateEndToEndId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * This validates the amount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     private function validateAmount() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $amount = $this->pixCashoutStaticQrCode->amount; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if (empty($amount) || !is_string($amount) || !is_numeric($amount) || $amount <= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             throw new \InvalidArgumentException('amount should be a numeric string and greater than zero'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * This validates a description | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 66 |  | View Code Duplication |     private function validateDescription() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         $description = $this->pixCashoutStaticQrCode->description; | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         if (empty($description) || !is_string($description)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             throw new \InvalidArgumentException('cashout description should be a string'); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * This validates a sender bank account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     private function validateSender() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if (!$this->pixCashoutStaticQrCode->sender instanceof BankAccount) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             throw new \InvalidArgumentException('sender should be a BankAccount'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->pixCashoutStaticQrCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             ->sender | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ->validate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * This validates a recipient bank account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     private function validateRecipient() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         if (!$this->pixCashoutStaticQrCode->recipient instanceof BankAccount) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             throw new \InvalidArgumentException('recipient should be a BankAccount'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $this->pixCashoutStaticQrCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ->recipient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             ->validate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * This validates a initialization type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 114 |  | View Code Duplication |     private function validateInitializationType() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $initializationType = $this->pixCashoutStaticQrCode->initializationType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         if (empty($initializationType) || !is_string($initializationType)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             throw new \InvalidArgumentException('initialization type should be a string'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         if ($this->pixCashoutStaticQrCode->initializationType != 'StaticQrCode') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             throw new \InvalidArgumentException('this initialization type is not valid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * This validates the end to end id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 132 |  | View Code Duplication |     private function validateEndToEndId() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $endToEndId = $this->pixCashoutStaticQrCode->endToEndId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         if (empty($endToEndId) || !is_string($endToEndId)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             throw new \InvalidArgumentException('end to end id should be a string'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |  | 
            
                        
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.