Completed
Pull Request — master (#554)
by Alejandro
11:37 queued 08:50
created

BackwardsCompatibleProblemDetailsMiddleware   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 92.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 65
ccs 25
cts 27
cp 0.9259
rs 10
c 1
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isVersionOne() 0 4 2
A __construct() 0 4 1
A mapStandardErrorTypes() 0 8 2
A process() 0 21 4
A makePayloadBackwardsCompatible() 0 6 1
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 10
    public function __construct(array $defaultTypeFallbacks, int $jsonFlags)
31
    {
32 10
        $this->defaultTypeFallbacks = $defaultTypeFallbacks;
33 10
        $this->jsonFlags = $jsonFlags;
34
    }
35
36 10
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
37
    {
38 10
        $resp = $handler->handle($request);
39 10
        if ($resp->getHeaderLine('Content-type') !== 'application/problem+json') {
40 2
            return $resp;
41
        }
42
43
        try {
44 8
            $body = (string) $resp->getBody();
45 8
            $payload = json_decode($body);
46
        } catch (Throwable $e) {
47
            return $resp;
48
        }
49
50 8
        $payload = $this->mapStandardErrorTypes($payload, $resp->getStatusCode());
51
52 8
        if ($this->isVersionOne($request)) {
0 ignored issues
show
Deprecated Code introduced by
The function Shlinkio\Shlink\Rest\Mid...dleware::isVersionOne() has been deprecated: When Shlink 2 is released, do not chekc the version ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

52
        if (/** @scrutinizer ignore-deprecated */ $this->isVersionOne($request)) {

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.

Loading history...
53 2
            $payload = $this->makePayloadBackwardsCompatible($payload);
0 ignored issues
show
Deprecated Code introduced by
The function Shlinkio\Shlink\Rest\Mid...adBackwardsCompatible() has been deprecated: When Shlink v2 is released, do not map old fields ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

53
            $payload = /** @scrutinizer ignore-deprecated */ $this->makePayloadBackwardsCompatible($payload);

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.

Loading history...
54
        }
55
56 8
        return new JsonResponse($payload, $resp->getStatusCode(), $resp->getHeaders(), $this->jsonFlags);
57
    }
58
59 8
    private function mapStandardErrorTypes(array $payload, int $respStatusCode): array
60
    {
61 8
        $type = $payload['type'] ?? '';
62 8
        if (strpos($type, 'https://httpstatus.es') === 0) {
63 3
            $payload['type'] = $this->defaultTypeFallbacks[$respStatusCode] ?? $type;
64
        }
65
66 8
        return $payload;
67
    }
68
69
    /** @deprecated When Shlink 2 is released, do not chekc the version */
70 8
    private function isVersionOne(ServerRequestInterface $request): bool
71
    {
72 8
        $path = $request->getUri()->getPath();
73 8
        return strpos($path, '/v') === false || strpos($path, '/v1') === 0;
74
    }
75
76
    /** @deprecated When Shlink v2 is released, do not map old fields */
77 2
    private function makePayloadBackwardsCompatible(array $payload): array
78
    {
79
        return reduce_left(self::BACKWARDS_COMPATIBLE_FIELDS, function (string $newKey, string $oldKey, $c, $acc) {
80 2
            $acc[$oldKey] = $acc[$newKey];
81 2
            return $acc;
82 2
        }, $payload);
83
    }
84
}
85