Completed
Pull Request — master (#1358)
by Guilh
07:06
created

ExceptionHandler::serializeToJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 7
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
use JMS\Serializer\Context;
15
use JMS\Serializer\GraphNavigator;
16
use JMS\Serializer\Handler\SubscribingHandlerInterface;
17
use JMS\Serializer\JsonSerializationVisitor;
18
use JMS\Serializer\XmlSerializationVisitor;
19
20
class ExceptionHandler extends AbstractExceptionNormalizer implements SubscribingHandlerInterface
21
{
22
    /**
23
     * @return array
24
     */
25 1
    public static function getSubscribingMethods()
26
    {
27
        return [
28
            [
29 1
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
30 1
                'format' => 'json',
31 1
                'type' => \Exception::class,
32 1
                'method' => 'serializeToJson',
33 1
            ],
34
            [
35 1
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
36 1
                'format' => 'xml',
37 1
                'type' => \Exception::class,
38 1
                'method' => 'serializeToXml',
39 1
            ],
40 1
        ];
41
    }
42
43
    /**
44
     * @param JsonSerializationVisitor $visitor
45
     * @param Exception                $exception
46
     * @param array                    $type
47
     * @param Context                  $context
48
     *
49
     * @return array
50
     */
51 1
    public function serializeToJson(
52
        JsonSerializationVisitor $visitor,
53
        \Exception $exception,
54
        array $type,
55
        Context $context
56
    ) {
57 1
        $data = $this->convertToArray($exception, $context);
58
59 1
        return $visitor->visitArray($data, $type, $context);
60
    }
61
62
    /**
63
     * @param XmlSerializationVisitor $visitor
64
     * @param Exception               $exception
65
     * @param array                   $type
66
     * @param Context                 $context
67
     */
68 1
    public function serializeToXml(
69
        XmlSerializationVisitor $visitor,
70
        \Exception $exception,
71
        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...
72
        Context $context
73
    ) {
74 1
        $data = $this->convertToArray($exception, $context);
75
76 1 View Code Duplication
        if (null === $visitor->document) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
77 1
            $visitor->document = $visitor->createDocument(null, null, true);
78 1
        }
79
80 1
        foreach ($data as $key => $value) {
81 1
            $entryNode = $visitor->document->createElement($key);
82 1
            $visitor->getCurrentNode()->appendChild($entryNode);
83 1
            $visitor->setCurrentNode($entryNode);
84
85 1
            $node = $context->getNavigator()->accept($value, null, $context);
86 1
            if (null !== $node) {
87 1
                $visitor->getCurrentNode()->appendChild($node);
88 1
            }
89
90 1
            $visitor->revertCurrentNode();
91 1
        }
92 1
    }
93
94
    /**
95
     * @param \Exception $exception
96
     *
97
     * @return array
98
     */
99 2
    protected function convertToArray(\Exception $exception, Context $context)
100
    {
101 2
        $data = [];
102
103 2
        $templateData = $context->attributes->get('template_data');
104 2
        if ($templateData->isDefined()) {
105 2
            $data['code'] = $statusCode = $templateData->get()['status_code'];
106 2
        }
107
108 2
        $data['message'] = $this->getExceptionMessage($exception, isset($statusCode) ? $statusCode : null);
109
110 2
        return $data;
111
    }
112
}
113