| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Ivory Http Adapter package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Eric GELOEN <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please read the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Ivory\HttpAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Ivory\HttpAdapter\Event\Events; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Ivory\HttpAdapter\Event\MultiRequestCreatedEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Ivory\HttpAdapter\Event\MultiRequestErroredEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Ivory\HttpAdapter\Event\MultiRequestSentEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Ivory\HttpAdapter\Event\RequestCreatedEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Ivory\HttpAdapter\Event\RequestErroredEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Ivory\HttpAdapter\Event\RequestSentEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Ivory\HttpAdapter\Message\InternalRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\EventDispatcher\EventDispatcherInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @author GeLo <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class EventDispatcherHttpAdapter extends PsrHttpAdapterDecorator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var EventDispatcherInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param PsrHttpAdapterInterface  $httpAdapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param EventDispatcherInterface $eventDispatcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 108 |  |     public function __construct(PsrHttpAdapterInterface $httpAdapter, EventDispatcherInterface $eventDispatcher) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 108 |  |         parent::__construct($httpAdapter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 108 |  |         $this->eventDispatcher = $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 108 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 63 |  |     protected function doSendInternalRequest(InternalRequestInterface $internalRequest) | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 63 |  |             $this->eventDispatcher->dispatch( | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 63 |  |                 Events::REQUEST_CREATED, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 63 |  |                 $requestCreatedEvent = new RequestCreatedEvent($this, $internalRequest) | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 49 |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 63 |  |             if ($requestCreatedEvent->hasException()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 9 |  |                 throw $requestCreatedEvent->getException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 54 |  |             $response = $requestCreatedEvent->hasResponse() | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 44 |  |                 ? $requestCreatedEvent->getResponse() | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 54 |  |                 : parent::doSendInternalRequest($requestCreatedEvent->getRequest()); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 36 |  |             $this->eventDispatcher->dispatch( | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 36 |  |                 Events::REQUEST_SENT, | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 36 |  |                 $requestSentEvent = new RequestSentEvent($this, $requestCreatedEvent->getRequest(), $response) | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 28 |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 36 |  |             if ($requestSentEvent->hasException()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 9 |  |                 throw $requestSentEvent->getException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 27 |  |             $response = $requestSentEvent->getResponse(); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 57 |  |         } catch (HttpAdapterException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 36 |  |             $e->setRequest($internalRequest); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 36 |  |             $e->setResponse(isset($response) ? $response : null); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 36 |  |             $this->eventDispatcher->dispatch( | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 36 |  |                 Events::REQUEST_ERRORED, | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 36 |  |                 $exceptionEvent = new RequestErroredEvent($this, $e) | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 28 |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 36 |  |             if ($exceptionEvent->hasResponse()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 9 |  |                 return $exceptionEvent->getResponse(); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 27 |  |             throw $exceptionEvent->getException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 27 |  |         return $response; | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 45 |  |     protected function doSendInternalRequests(array $internalRequests) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 45 |  |         $responses = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 45 |  |         $exceptions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 101 | 45 | View Code Duplication |         if (!empty($internalRequests)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 45 |  |             $this->eventDispatcher->dispatch( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 45 |  |                 Events::MULTI_REQUEST_CREATED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 45 |  |                 $multiRequestCreatedEvent = new MultiRequestCreatedEvent($this, $internalRequests) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 35 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 45 |  |             $internalRequests = $multiRequestCreatedEvent->getRequests(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 45 |  |             $responses = $multiRequestCreatedEvent->getResponses(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 45 |  |             $exceptions = $multiRequestCreatedEvent->getExceptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 35 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 45 |  |             $responses = array_merge($responses, parent::doSendInternalRequests($internalRequests)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 39 |  |         } catch (MultiHttpAdapterException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 18 |  |             $responses = array_merge($responses, $e->getResponses()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 18 |  |             $exceptions = array_merge($exceptions, $e->getExceptions()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 119 | 45 | View Code Duplication |         if (!empty($responses)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 27 |  |             $this->eventDispatcher->dispatch( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 27 |  |                 Events::MULTI_REQUEST_SENT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 27 |  |                 $requestSentEvent = new MultiRequestSentEvent($this, $responses) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 21 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 27 |  |             $exceptions = array_merge($exceptions, $requestSentEvent->getExceptions()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 27 |  |             $responses = $requestSentEvent->getResponses(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 21 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 45 |  |         if (!empty($exceptions)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 27 |  |             $this->eventDispatcher->dispatch( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 27 |  |                 Events::MULTI_REQUEST_ERRORED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 27 |  |                 $exceptionEvent = new MultiRequestErroredEvent($this, $exceptions) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 21 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 27 |  |             $responses = array_merge($responses, $exceptionEvent->getResponses()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 27 |  |             $exceptions = $exceptionEvent->getExceptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 27 |  |             if (!empty($exceptions)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 18 |  |                 throw new MultiHttpAdapterException($exceptions, $responses); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 7 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 27 |  |         return $responses; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 145 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |  | 
            
                        
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.