Completed
Push — master ( e1388e...5e3850 )
by
unknown
19:37
created

ExceptionHandler::serializeToXml()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 25

Duplication

Lines 3
Ratio 12 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
dl 3
loc 25
ccs 16
cts 16
cp 1
rs 9.52
c 0
b 0
f 0
cc 4
nc 6
nop 4
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\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 2
    public static function getSubscribingMethods()
26
    {
27
        return [
28
            [
29 2
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
30 2
                'format' => 'json',
31 2
                'type' => \Exception::class,
32 2
                'method' => 'serializeToJson',
33 2
            ],
34
            [
35 2
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
36 2
                'format' => 'xml',
37 2
                'type' => \Exception::class,
38 2
                'method' => 'serializeToXml',
39 2
            ],
40 2
        ];
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 2
    public function serializeToJson(
52
        JsonSerializationVisitor $visitor,
53
        \Exception $exception,
54
        array $type,
55
        Context $context
56
    ) {
57 2
        $data = $this->convertToArray($exception, $context);
58
59 2
        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) {
77 1
            $visitor->document = $visitor->createDocument(null, null, true);
0 ignored issues
show
Deprecated Code introduced by
The method JMS\Serializer\XmlSerial...sitor::createDocument() has been deprecated with message: Use $this->getDocument(true) instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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
     * @param Context    $context
97
     *
98
     * @return array
99 3
     */
100
    protected function convertToArray(\Exception $exception, Context $context)
101 3
    {
102
        $data = [];
103 3
104 3
        $templateData = $context->attributes->get('template_data');
0 ignored issues
show
Deprecated Code introduced by
The property JMS\Serializer\Context::$attributes has been deprecated with message: use has/get/set attribute methods

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
105 3
        if ($templateData->isDefined()) {
106 3
            $templateData = $templateData->get();
107
            if (array_key_exists('status_code', $templateData)) {
108 3
                $data['code'] = $statusCode = $templateData['status_code'];
109
            }
110 3
        }
111
112
        $data['message'] = $this->getExceptionMessage($exception, isset($statusCode) ? $statusCode : null);
113
114
        return $data;
115
    }
116
}
117