Completed
Push — master ( 6744a4...ab6d26 )
by Guilh
7s
created

FormErrorHandler::serializeFormToJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 4
crap 2
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\Handler\FormErrorHandler as JMSFormErrorHandler;
16
use JMS\Serializer\JsonSerializationVisitor;
17
use JMS\Serializer\XmlSerializationVisitor;
18
use Symfony\Component\Form\Form;
19
use JMS\Serializer\YamlSerializationVisitor;
20
use Symfony\Component\Translation\TranslatorInterface;
21
22
/**
23
 * Extend the JMS FormErrorHandler to include more informations when using the ViewHandler.
24
 */
25
class FormErrorHandler extends JMSFormErrorHandler
26
{
27
    private $translator;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
28
29 2
    public function __construct(TranslatorInterface $translator)
30
    {
31 2
        $this->translator = $translator;
32 2
        parent::__construct($translator);
33 2
    }
34
35 1
    public function serializeFormToXml(XmlSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
36
    {
37 1
        if ($context) {
38 1
            $statusCode = $context->attributes->get('status_code');
39 1
            if ($statusCode->isDefined()) {
40 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...
41 1
                    $visitor->document = $visitor->createDocument(null, null, true);
42 1
                }
43
44 1
                $codeNode = $visitor->document->createElement('code');
45 1
                $visitor->getCurrentNode()->appendChild($codeNode);
46 1
                $codeNode->appendChild($context->getNavigator()->accept($statusCode->get(), null, $context));
47
48 1
                $messageNode = $visitor->document->createElement('message');
49 1
                $visitor->getCurrentNode()->appendChild($messageNode);
50 1
                $messageNode->appendChild($context->getNavigator()->accept('Validation Failed', null, $context));
51
52 1
                $errorsNode = $visitor->document->createElement('errors');
53 1
                $visitor->getCurrentNode()->appendChild($errorsNode);
54 1
                $visitor->setCurrentNode($errorsNode);
55 1
                parent::serializeFormToXml($visitor, $form, $type);
56 1
                $visitor->revertCurrentNode();
57
58 1
                return;
59
            }
60
        }
61
62 1
        return parent::serializeFormToXml($visitor, $form, $type);
63
    }
64
65 1 View Code Duplication
    public function serializeFormToJson(JsonSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
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...
66
    {
67 1
        $isRoot = null === $visitor->getRoot();
68 1
        $result = $this->adaptFormArray(parent::serializeFormToJson($visitor, $form, $type), $context);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->adaptFormArray(pa...orm, $type), $context); of type array|ArrayObject adds the type array to the return on line 74 which is incompatible with the return type of the parent method JMS\Serializer\Handler\F...er::serializeFormToJson of type ArrayObject.
Loading history...
69
70 1
        if ($isRoot) {
71 1
            $visitor->setRoot($result);
72 1
        }
73
74 1
        return $result;
75
    }
76
77 View Code Duplication
    public function serializeFormToYml(YamlSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
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...
78
    {
79
        $isRoot = null === $visitor->getRoot();
0 ignored issues
show
Bug introduced by
The method getRoot() does not seem to exist on object<JMS\Serializer\YamlSerializationVisitor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
        $result = $this->adaptFormArray(parent::serializeFormToYml($visitor, $form, $type), $context);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->adaptFormArray(pa...orm, $type), $context); of type array|ArrayObject adds the type array to the return on line 86 which is incompatible with the return type of the parent method JMS\Serializer\Handler\F...ler::serializeFormToYml of type ArrayObject.
Loading history...
81
82
        if ($isRoot) {
83
            $visitor->setRoot($result);
0 ignored issues
show
Bug introduced by
The method setRoot() does not seem to exist on object<JMS\Serializer\YamlSerializationVisitor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
        }
85
86
        return $result;
87
    }
88
89 1
    private function adaptFormArray(\ArrayObject $serializedForm, Context $context = null)
90
    {
91 1
        $statusCode = $this->getStatusCode($context);
92 1
        if (null !== $statusCode) {
93
            return [
94 1
                'code' => $statusCode,
95 1
                'message' => 'Validation Failed',
96 1
                'errors' => $serializedForm,
97 1
            ];
98
        }
99
100
        return $serializedForm;
101
    }
102
103 1
    private function getStatusCode(Context $context = null)
104
    {
105 1
        if (null === $context) {
106
            return;
107
        }
108
109 1
        $statusCode = $context->attributes->get('status_code');
110 1
        if ($statusCode->isDefined()) {
111 1
            return $statusCode->get();
112
        }
113
    }
114
}
115