Completed
Push — master ( eefa71...7d5e7c )
by Christian
02:51 queued 10s
created

FlattenExceptionNormalizer::normalize()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 36

Duplication

Lines 5
Ratio 13.89 %

Code Coverage

Tests 18
CRAP Score 7.0071

Importance

Changes 0
Metric Value
dl 5
loc 36
ccs 18
cts 19
cp 0.9474
rs 8.4106
c 0
b 0
f 0
cc 7
nc 16
nop 3
crap 7.0071
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Serializer\Normalizer;
13
14
use FOS\RestBundle\Util\ExceptionValueMap;
15
use Symfony\Component\ErrorHandler\Exception\FlattenException;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
18
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
19
20
/**
21
 * @author Christian Flothmann <[email protected]>
22
 *
23
 * @internal
24
 */
25
final class FlattenExceptionNormalizer implements CacheableSupportsMethodInterface, NormalizerInterface
26
{
27
    private $messagesMap;
28
    private $debug;
29
    private $rfc7807;
30
31 9
    public function __construct(ExceptionValueMap $messagesMap, bool $debug, bool $rfc7807)
32
    {
33 9
        $this->messagesMap = $messagesMap;
34 9
        $this->debug = $debug;
35 9
        $this->rfc7807 = $rfc7807;
36 9
    }
37
38 7
    public function normalize($exception, $format = null, array $context = [])
39
    {
40 7
        if (isset($context['status_code'])) {
41
            $statusCode = $context['status_code'];
42
        } else {
43 7
            $statusCode = $exception->getStatusCode();
44
        }
45
46 7
        $showMessage = $this->messagesMap->resolveFromClassName($exception->getClass());
47
48 7 View Code Duplication
        if ($showMessage || $this->debug) {
49 6
            $message = $exception->getMessage();
50
        } else {
51 1
            $message = Response::$statusTexts[$statusCode] ?? 'error';
52
        }
53
54 7
        if ($this->rfc7807) {
55 2
            if ('json' === $format) {
56 1
                $exception->setHeaders($exception->getHeaders() + ['Content-Type' => 'application/problem+json']);
57 1
            } elseif ('xml' === $format) {
58 1
                $exception->setHeaders($exception->getHeaders() + ['Content-Type' => 'application/problem+xml']);
59
            }
60
61
            return [
62 2
                'type' => $context['type'] ?? 'https://tools.ietf.org/html/rfc2616#section-10',
63 2
                'title' => $context['title'] ?? 'An error occurred',
64 2
                'status' => $statusCode,
65 2
                'detail' => $message,
66
            ];
67
        } else {
68
            return [
69 5
                'code' => $statusCode,
70 5
                'message' => $message,
71
            ];
72
        }
73
    }
74
75 9
    public function supportsNormalization($data, $format = null)
76
    {
77 9
        return $data instanceof FlattenException;
78
    }
79
80 9
    public function hasCacheableSupportsMethod(): bool
81
    {
82 9
        return true;
83
    }
84
}
85