| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Shlinkio\Shlink\Rest\Middleware; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Psr\Http\Message\ServerRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Psr\Http\Server\MiddlewareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Psr\Http\Server\RequestHandlerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Zend\Diactoros\Response\JsonResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use function Functional\reduce_left; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use function Shlinkio\Shlink\Common\json_decode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use function strpos; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | class BackwardsCompatibleProblemDetailsMiddleware implements MiddlewareInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     private const BACKWARDS_COMPATIBLE_FIELDS = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         'error' => 'type', | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         'message' => 'detail', | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     /** @var array */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     private $defaultTypeFallbacks; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** @var int */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     private $jsonFlags; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     public function __construct(array $defaultTypeFallbacks, int $jsonFlags) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $this->defaultTypeFallbacks = $defaultTypeFallbacks; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $this->jsonFlags = $jsonFlags; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $resp = $handler->handle($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         if ($resp->getHeaderLine('Content-type') !== 'application/problem+json') { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             return $resp; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             $body = (string) $resp->getBody(); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             $payload = json_decode($body); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         } catch (Throwable $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             return $resp; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $payload = $this->mapStandardErrorTypes($payload, $resp->getStatusCode()); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         if ($this->isVersionOne($request)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             $payload = $this->makePayloadBackwardsCompatible($payload); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         return new JsonResponse($payload, $resp->getStatusCode(), $resp->getHeaders(), $this->jsonFlags); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     private function mapStandardErrorTypes(array $payload, int $respStatusCode): array | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $type = $payload['type'] ?? ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         if (strpos($type, 'https://httpstatus.es') === 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             $payload['type'] = $this->defaultTypeFallbacks[$respStatusCode] ?? $type; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         return $payload; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     /** @deprecated When Shlink 2 is released, do not chekc the version */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     private function isVersionOne(ServerRequestInterface $request): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $path = $request->getUri()->getPath(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |         return strpos($path, '/v') === false || strpos($path, '/v1') === 0; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 76 |  |  |     /** @deprecated When Shlink v2 is released, do not map old fields */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     private function makePayloadBackwardsCompatible(array $payload): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return reduce_left(self::BACKWARDS_COMPATIBLE_FIELDS, function (string $newKey, string $oldKey, $c, $acc) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $acc[$oldKey] = $acc[$newKey]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             return $acc; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |         }, $payload); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  | 
            
                        
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.