Completed
Pull Request — 2.x (#2253)
by Christian
04:06
created

FlattenExceptionNormalizer::normalize()   B

Complexity

Conditions 8
Paths 24

Size

Total Lines 36

Duplication

Lines 10
Ratio 27.78 %

Code Coverage

Tests 19
CRAP Score 8.0079

Importance

Changes 0
Metric Value
dl 10
loc 36
ccs 19
cts 20
cp 0.95
rs 8.0995
c 0
b 0
f 0
cc 8
nc 24
nop 3
crap 8.0079
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\NormalizerInterface;
18
19
/**
20
 * @author Christian Flothmann <[email protected]>
21
 *
22
 * @internal
23
 */
24
final class FlattenExceptionNormalizer implements NormalizerInterface
25
{
26
    private $statusCodeMap;
27
    private $messagesMap;
28
    private $debug;
29
    private $rfc7807;
30
31 31 View Code Duplication
    public function __construct(ExceptionValueMap $statusCodeMap, ExceptionValueMap $messagesMap, bool $debug, bool $rfc7807)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
32
    {
33 31
        $this->statusCodeMap = $statusCodeMap;
34 31
        $this->messagesMap = $messagesMap;
35 31
        $this->debug = $debug;
36 31
        $this->rfc7807 = $rfc7807;
37 31
    }
38
39 13
    public function normalize($exception, $format = null, array $context = [])
40
    {
41 13 View Code Duplication
        if (isset($context['status_code'])) {
42
            $statusCode = $context['status_code'];
43 13
        } elseif (false === $statusCode = $this->statusCodeMap->resolveFromClassName($exception->getClass())) {
44 11
            $statusCode = $exception->getStatusCode();
45
        }
46
47 13
        $showMessage = $this->messagesMap->resolveFromClassName($exception->getClass());
48
49 13 View Code Duplication
        if ($showMessage || $this->debug) {
50 12
            $message = $exception->getMessage();
51
        } else {
52 1
            $message = Response::$statusTexts[$statusCode] ?? 'error';
53
        }
54
55 13
        if ($this->rfc7807) {
56 3
            if ('json' === $format) {
57 2
                $exception->setHeaders($exception->getHeaders() + ['Content-Type' => 'application/problem+json']);
58 1
            } elseif ('xml' === $format) {
59 1
                $exception->setHeaders($exception->getHeaders() + ['Content-Type' => 'application/problem+xml']);
60
            }
61
62
            return [
63 3
                'type' => $context['type'] ?? 'https://tools.ietf.org/html/rfc2616#section-10',
64 3
                'title' => $context['title'] ?? 'An error occurred',
65 3
                'status' => $statusCode,
66 3
                'detail' => $message,
67
            ];
68
        } else {
69
            return [
70 10
                'code' => $statusCode,
71 10
                'message' => $message,
72
            ];
73
        }
74
    }
75
76 20
    public function supportsNormalization($data, $format = null)
77
    {
78 20
        return $data instanceof FlattenException;
79
    }
80
}
81