Completed
Pull Request — 2.x (#2161)
by Christian
03:47
created

ExceptionHandler::serializeToJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 3
cts 3
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
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 1
@trigger_error(sprintf('The %s\ExceptionHandler class is deprecated since FOSRestBundle 2.8.', __NAMESPACE__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
15
16
use JMS\Serializer\Context;
17
use JMS\Serializer\GraphNavigatorInterface;
18
use JMS\Serializer\Handler\SubscribingHandlerInterface;
19
use JMS\Serializer\JsonSerializationVisitor;
20
use JMS\Serializer\XmlSerializationVisitor;
21
22
/**
23
 * @deprecated since 2.8
24
 */
25
class ExceptionHandler extends AbstractExceptionNormalizer implements SubscribingHandlerInterface
26
{
27 2
    public static function getSubscribingMethods()
28
    {
29
        return [
30
            [
31 2
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
32
                'format' => 'json',
33
                'type' => \Error::class,
34
                'method' => 'serializeErrorToJson',
35
            ],
36
            [
37
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
38
                'format' => 'json',
39
                'type' => \Exception::class,
40
                'method' => 'serializeToJson',
41
            ],
42
            [
43
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
44
                'format' => 'xml',
45
                'type' => \Error::class,
46
                'method' => 'serializeErrorToXml',
47
            ],
48
            [
49
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
50
                'format' => 'xml',
51
                'type' => \Exception::class,
52
                'method' => 'serializeToXml',
53
            ],
54
        ];
55
    }
56
57 2
    public function serializeToJson(
58
        JsonSerializationVisitor $visitor,
59
        \Exception $exception,
60
        array $type,
61
        Context $context
62
    ) {
63 2
        $data = $this->convertToArray($exception, $context);
64
65 2
        return $visitor->visitArray($data, $type, $context);
0 ignored issues
show
Unused Code introduced by
The call to JsonSerializationVisitor::visitArray() has too many arguments starting with $context.

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...
66
    }
67
68
    public function serializeErrorToJson(
69
        JsonSerializationVisitor $visitor,
70
        \Throwable $exception,
71
        array $type,
72
        Context $context
73
    ) {
74
        $data = $this->convertThrowableToArray($exception, $context);
75
76
        return $visitor->visitArray($data, $type, $context);
0 ignored issues
show
Unused Code introduced by
The call to JsonSerializationVisitor::visitArray() has too many arguments starting with $context.

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...
77
    }
78
79 1 View Code Duplication
    public function serializeToXml(
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...
80
        XmlSerializationVisitor $visitor,
81
        \Exception $exception,
82
        array $type,
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
        Context $context
84
    ) {
85 1
        $data = $this->convertToArray($exception, $context);
86
87 1
        $document = $visitor->getDocument(true);
0 ignored issues
show
Unused Code introduced by
The call to XmlSerializationVisitor::getDocument() has too many arguments starting with true.

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...
88
89 1
        if (!$visitor->getCurrentNode()) {
90 1
            $visitor->createRoot();
91
        }
92
93 1
        foreach ($data as $key => $value) {
94 1
            $entryNode = $document->createElement($key);
95 1
            $visitor->getCurrentNode()->appendChild($entryNode);
96 1
            $visitor->setCurrentNode($entryNode);
97
98 1
            $node = $context->getNavigator()->accept($value, null, $context);
0 ignored issues
show
Unused Code introduced by
The call to GraphNavigatorInterface::accept() has too many arguments starting with $context.

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...
99 1
            if (null !== $node) {
100 1
                $visitor->getCurrentNode()->appendChild($node);
101
            }
102
103 1
            $visitor->revertCurrentNode();
104
        }
105 1
    }
106
107 View Code Duplication
    public function serializeErrorToXml(
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...
108
        XmlSerializationVisitor $visitor,
109
        \Throwable $exception,
110
        array $type,
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
        Context $context
112
    ) {
113
        $data = $this->convertThrowableToArray($exception, $context);
114
115
        $document = $visitor->getDocument(true);
0 ignored issues
show
Unused Code introduced by
The call to XmlSerializationVisitor::getDocument() has too many arguments starting with true.

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...
116
117
        if (!$visitor->getCurrentNode()) {
118
            $visitor->createRoot();
119
        }
120
121
        foreach ($data as $key => $value) {
122
            $entryNode = $document->createElement($key);
123
            $visitor->getCurrentNode()->appendChild($entryNode);
124
            $visitor->setCurrentNode($entryNode);
125
126
            $node = $context->getNavigator()->accept($value, null, $context);
0 ignored issues
show
Unused Code introduced by
The call to GraphNavigatorInterface::accept() has too many arguments starting with $context.

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...
127
            if (null !== $node) {
128
                $visitor->getCurrentNode()->appendChild($node);
129
            }
130
131
            $visitor->revertCurrentNode();
132
        }
133
    }
134
135 3
    protected function convertToArray(\Exception $exception, Context $context): array
136
    {
137 3
        return $this->convertThrowableToArray($exception, $context);
138
    }
139
140 3
    private function convertThrowableToArray(\Throwable $throwable, Context $context): array
141
    {
142 3
        $data = [];
143
144 3
        if ($context->hasAttribute('template_data')) {
145 3
            $templateData = $context->getAttribute('template_data');
146
147 3
            if (array_key_exists('status_code', $templateData)) {
148 3
                $data['code'] = $statusCode = $templateData['status_code'];
149
            } elseif ($context->hasAttribute('status_code')) {
150 3
                $data['code'] = $context->getAttribute('status_code');
151
            }
152
        } elseif ($context->hasAttribute('status_code')) {
153
            $data['code'] = $context->getAttribute('status_code');
154
        }
155
156 3
        $data['message'] = $this->getMessageFromThrowable($throwable, isset($statusCode) ? $statusCode : null);
157
158 3
        return $data;
159
    }
160
}
161