Completed
Push — master ( 4d453b...dbe76a )
by Christian
02:29 queued 11s
created

SerializerErrorRenderer::render()   A

Complexity

Conditions 4
Paths 12

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 12
nop 1
crap 4
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\ErrorRenderer;
13
14
use FOS\RestBundle\Context\Context;
15
use FOS\RestBundle\Serializer\Serializer;
16
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
17
use Symfony\Component\ErrorHandler\Exception\FlattenException;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
20
21
/**
22
 * @internal
23
 */
24
final class SerializerErrorRenderer implements ErrorRendererInterface
25
{
26
    private $serializer;
27
    private $format;
28
    private $fallbackErrorRenderer;
29
    private $debug;
30
31
    /**
32
     * @param string|callable(FlattenException) $format
0 ignored issues
show
Documentation introduced by
The doc-type string|callable(FlattenException) could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
33
     * @param string|bool                       $debug
34
     */
35 8
    public function __construct(Serializer $serializer, $format, ErrorRendererInterface $fallbackErrorRenderer = null, $debug = false)
36
    {
37 8
        if (!is_string($format) && !is_callable($format)) {
38
            throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be a string or a callable, "%s" given.', __METHOD__, \is_object($format) ? \get_class($format) : \gettype($format)));
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('Argument 2 pass...t) : \gettype($format)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
39
        }
40
41 8
        if (!is_bool($debug) && !is_callable($debug)) {
42
            throw new \TypeError(sprintf('Argument 4 passed to "%s()" must be a boolean or a callable, "%s" given.', __METHOD__, \is_object($debug) ? \get_class($debug) : \gettype($debug)));
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('Argument 4 pass...ug) : \gettype($debug)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
43
        }
44
45 8
        $this->serializer = $serializer;
46 8
        $this->format = $format;
47 8
        $this->fallbackErrorRenderer = $fallbackErrorRenderer;
48 8
        $this->debug = $debug;
49 8
    }
50
51 8
    public function render(\Throwable $exception): FlattenException
52
    {
53 8
        $flattenException = FlattenException::createFromThrowable($exception);
54
55
        try {
56 8
            $format = is_callable($this->format) ? ($this->format)($flattenException) : $this->format;
57
58 8
            $context = new Context();
59 8
            $context->setAttribute('exception', $exception);
60 8
            $context->setAttribute('debug', is_callable($this->debug) ? ($this->debug)($exception) : $this->debug);
61
62 8
            return $flattenException->setAsString($this->serializer->serialize($flattenException, $format, $context));
63 1
        } catch (NotEncodableValueException $e) {
64 1
            return $this->fallbackErrorRenderer->render($exception);
65
        }
66
    }
67
68
    /**
69
     * @see \Symfony\Component\ErrorHandler\ErrorRenderer\SerializerErrorRenderer::getPreferredFormat
70
     */
71 5
    public static function getPreferredFormat(RequestStack $requestStack): \Closure
72
    {
73
        return static function () use ($requestStack) {
74 5
            if (!$request = $requestStack->getCurrentRequest()) {
75
                throw new NotEncodableValueException();
76
            }
77
78 5
            return $request->getPreferredFormat();
79 5
        };
80
    }
81
82
    /**
83
     * @see \Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer::isDebug
84
     */
85 4
    public static function isDebug(RequestStack $requestStack, bool $debug): \Closure
86
    {
87
        return static function () use ($requestStack, $debug): bool {
88 4
            if (!$request = $requestStack->getCurrentRequest()) {
89
                return $debug;
90
            }
91
92 4
            return $debug && $request->attributes->getBoolean('showException', true);
93 4
        };
94
    }
95
}
96