| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of Phiremock. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Phiremock is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * it under the terms of the GNU Lesser General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Phiremock is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * GNU General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * You should have received a copy of the GNU General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * along with Phiremock.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | namespace Mcustiel\Phiremock\Common\Utils\V2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Mcustiel\Phiremock\Domain\Options\Delay; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Mcustiel\Phiremock\Domain\Options\ScenarioState; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Mcustiel\Phiremock\Domain\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | abstract class ArrayToResponseConverter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     const ALLOWED_OPTIONS = ['delayMillis'=> null]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     const NO_DELAY = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function convert(array $responseArray): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return $this->convertResponse( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             $responseArray, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $this->getDelay($responseArray), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             $this->getNewScenarioState($responseArray) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected function ensureNotInvalidOptionsAreProvided(array $responseArray): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $invalidOptions = array_diff_key($responseArray, static::ALLOWED_OPTIONS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         if (!empty($invalidOptions)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             throw new \Exception('Unknown expectation options: ' . var_export($invalidOptions, true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     abstract protected function convertResponse( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         array $response, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ?Delay $delay, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         ?ScenarioState $newScenarioState | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     ): Response; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     private function getDelay(array $responseArray): ?Delay | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         if (empty($responseArray['response'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $responseArray = $responseArray['response']; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         if (!empty($responseArray['delayMillis'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             return new Delay($responseArray['delayMillis']); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     private function getNewScenarioState(array $expectationArray): ?ScenarioState | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $newScenarioState = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if (!empty($expectationArray['newScenarioState'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $newScenarioState = new ScenarioState($expectationArray['newScenarioState']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return $newScenarioState; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  |