FormErrorHandler::serializeFormToXml()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4.002

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 19
cts 20
cp 0.95
rs 9.408
c 0
b 0
f 0
cc 4
nc 4
nop 4
crap 4.002
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\Handler\SubscribingHandlerInterface;
17
use JMS\Serializer\JsonSerializationVisitor;
18
use JMS\Serializer\Visitor\SerializationVisitorInterface;
19
use JMS\Serializer\XmlSerializationVisitor;
20
use Symfony\Component\Form\Form;
21
use JMS\Serializer\YamlSerializationVisitor;
22
23
/**
24
 * Extend the JMS FormErrorHandler to include more information when using the ViewHandler.
25
 *
26
 * @internal
27
 */
28
class FormErrorHandler implements SubscribingHandlerInterface
29
{
30
    private $formErrorHandler;
31
32 3
    public function __construct(JMSFormErrorHandler $formErrorHandler)
33
    {
34 3
        $this->formErrorHandler = $formErrorHandler;
35 3
    }
36
37 4
    public static function getSubscribingMethods(): array
38
    {
39 4
        return JMSFormErrorHandler::getSubscribingMethods();
40
    }
41
42 1
    public function serializeFormToXml(XmlSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
43
    {
44 1
        if ($context) {
45 1
            if ($context->hasAttribute('status_code')) {
46 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...
47 1
                if (!$visitor->getCurrentNode()) {
48 1
                    $visitor->createRoot();
49
                }
50
51 1
                $codeNode = $document->createElement('code');
52 1
                $visitor->getCurrentNode()->appendChild($codeNode);
53 1
                $codeNode->appendChild($context->getNavigator()->accept($context->getAttribute('status_code'), 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...
54
55 1
                $messageNode = $document->createElement('message');
56 1
                $visitor->getCurrentNode()->appendChild($messageNode);
57 1
                $messageNode->appendChild($context->getNavigator()->accept('Validation Failed', 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...
58
59 1
                $errorsNode = $document->createElement('errors');
60 1
                $visitor->getCurrentNode()->appendChild($errorsNode);
61 1
                $visitor->setCurrentNode($errorsNode);
62
63 1
                $errorNodes = $this->formErrorHandler->serializeFormToXml($visitor, $form, $type);
64 1
                $errorsNode->appendChild($errorNodes);
65
66 1
                $visitor->revertCurrentNode();
67
68 1
                return $visitor->getCurrentNode();
69
            }
70
        }
71
72
        return $this->formErrorHandler->serializeFormToXml($visitor, $form, $type);
73
    }
74
75 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...
76
    {
77 1
        $isRoot = !interface_exists(SerializationVisitorInterface::class) && null === $visitor->getRoot();
0 ignored issues
show
Bug introduced by
The method getRoot() does not seem to exist on object<JMS\Serializer\JsonSerializationVisitor>.

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...
78 1
        $result = $this->adaptFormArray($this->formErrorHandler->serializeFormToJson($visitor, $form, $type), $context);
79
80 1
        if ($isRoot) {
81
            $visitor->setRoot($result);
0 ignored issues
show
Bug introduced by
The method setRoot() does not seem to exist on object<JMS\Serializer\JsonSerializationVisitor>.

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...
82
        }
83
84 1
        return $result;
85
    }
86
87 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...
88
    {
89
        $isRoot = null === $visitor->getRoot();
90
        $result = $this->adaptFormArray($this->formErrorHandler->serializeFormToYml($visitor, $form, $type), $context);
0 ignored issues
show
Bug introduced by
The method serializeFormToYml() does not exist on JMS\Serializer\Handler\FormErrorHandler. Did you maybe mean serializeFormToXml()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
91
92
        if ($isRoot) {
93
            $visitor->setRoot($result);
94
        }
95
96
        return $result;
97
    }
98
99
    public function __call($name, $arguments)
100
    {
101
        return call_user_func_array([$this->formErrorHandler, $name], $arguments);
102
    }
103
104 1
    private function adaptFormArray(\ArrayObject $serializedForm, Context $context = null)
105
    {
106 1
        $statusCode = $this->getStatusCode($context);
107 1
        if (null !== $statusCode) {
108
            return [
109 1
                'code' => $statusCode,
110 1
                'message' => 'Validation Failed',
111 1
                'errors' => $serializedForm,
112
            ];
113
        }
114
115
        return $serializedForm;
116
    }
117
118 1
    private function getStatusCode(Context $context = null)
119
    {
120 1
        if (null === $context) {
121
            return;
122
        }
123
124 1
        if ($context->hasAttribute('status_code')) {
125 1
            return $context->getAttribute('status_code');
126
        }
127
    }
128
}
129