| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Chubbyphp\ErrorHandler\Slim; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Chubbyphp\ErrorHandler\ContentTypeResolverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Chubbyphp\ErrorHandler\ErrorResponseProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Psr\Http\Message\ServerRequestInterface as Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Psr\Http\Message\ResponseInterface as Response; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | final class AdvancedErrorHandler implements ErrorHandlerInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |      * @var ContentTypeResolverInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     private $contentTypeResolver; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @var ErrorResponseProviderInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     private $fallbackProvider; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @var ErrorResponseProviderInterface[] | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     private $providers = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @param ContentTypeResolverInterface   $contentTypeResolver | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @param ErrorResponseProviderInterface $fallbackProvider | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param array                          $providers | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         ContentTypeResolverInterface $contentTypeResolver, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         ErrorResponseProviderInterface $fallbackProvider, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         array $providers = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->contentTypeResolver = $contentTypeResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->fallbackProvider = $fallbackProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->addProvider($fallbackProvider); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         foreach ($providers as $provider) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $this->addProvider($provider); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @param ErrorResponseProviderInterface $provider | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     private function addProvider(ErrorResponseProviderInterface $provider) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->providers[$provider->getContentType()] = $provider; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @param Request    $request | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @param Response   $response | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @param \Exception $exception | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * @return Response | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @throws \LogicException | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function __invoke(Request $request, Response $response, \Exception $exception): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $contentType = $this->contentTypeResolver->getContentType($request, array_keys($this->providers)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if (isset($this->providers[$contentType])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             return $this->providers[$contentType]->get($request, $response, $exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return $this->fallbackProvider->get($request, $response, $exception); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  |