Completed
Push — master ( 18870f...93a06c )
by
unknown
06:00
created

FormErrorHandler::getStatusCode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
ccs 0
cts 1
cp 0
cc 3
eloc 6
nc 3
nop 1
crap 12
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\XmlSerializationVisitor;
19
use Symfony\Component\Form\Form;
20
use JMS\Serializer\YamlSerializationVisitor;
21
22
/**
23
 * Extend the JMS FormErrorHandler to include more informations when using the ViewHandler.
24
 */
25
class FormErrorHandler implements SubscribingHandlerInterface
26
{
27
    private $formErrorHandler;
28
29 2
    public function __construct(JMSFormErrorHandler $formErrorHandler)
30
    {
31 2
        $this->formErrorHandler = $formErrorHandler;
32 2
    }
33 2
34
    public static function getSubscribingMethods()
35 1
    {
36
        return JMSFormErrorHandler::getSubscribingMethods();
37 1
    }
38 1
39 1
    public function serializeFormToXml(XmlSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
40 1
    {
41 1
        if ($context) {
42 1
            $statusCode = $context->attributes->get('status_code');
43
            if ($statusCode->isDefined()) {
44 1 View Code Duplication
                if (null === $visitor->document) {
45 1
                    $visitor->document = $visitor->createDocument(null, null, true);
46 1
                }
47
48 1
                $codeNode = $visitor->document->createElement('code');
49 1
                $visitor->getCurrentNode()->appendChild($codeNode);
50 1
                $codeNode->appendChild($context->getNavigator()->accept($statusCode->get(), null, $context));
51
52 1
                $messageNode = $visitor->document->createElement('message');
53 1
                $visitor->getCurrentNode()->appendChild($messageNode);
54 1
                $messageNode->appendChild($context->getNavigator()->accept('Validation Failed', null, $context));
55 1
56 1
                $errorsNode = $visitor->document->createElement('errors');
57
                $visitor->getCurrentNode()->appendChild($errorsNode);
58 1
                $visitor->setCurrentNode($errorsNode);
59
                $this->formErrorHandler->serializeFormToXml($visitor, $form, $type);
60
                $visitor->revertCurrentNode();
61
62 1
                return;
63
            }
64
        }
65 1
66
        return $this->formErrorHandler->serializeFormToXml($visitor, $form, $type);
67 1
    }
68 1
69 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...
70 1
    {
71 1
        $isRoot = null === $visitor->getRoot();
72 1
        $result = $this->adaptFormArray($this->formErrorHandler->serializeFormToJson($visitor, $form, $type), $context);
73
74 1
        if ($isRoot) {
75
            $visitor->setRoot($result);
76
        }
77
78
        return $result;
79
    }
80
81 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...
82
    {
83
        $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...
84
        $result = $this->adaptFormArray($this->formErrorHandler->serializeFormToYml($visitor, $form, $type), $context);
85
86
        if ($isRoot) {
87
            $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...
88
        }
89 1
90
        return $result;
91 1
    }
92 1
93
    public function __call($name, $arguments)
94 1
    {
95 1
        return call_user_func_array([$this->formErrorHandler, $name], $arguments);
96 1
    }
97 1
98
    private function adaptFormArray(\ArrayObject $serializedForm, Context $context = null)
99
    {
100
        $statusCode = $this->getStatusCode($context);
101
        if (null !== $statusCode) {
102
            return [
103 1
                'code' => $statusCode,
104
                'message' => 'Validation Failed',
105 1
                'errors' => $serializedForm,
106
            ];
107
        }
108
109 1
        return $serializedForm;
110 1
    }
111 1
112
    private function getStatusCode(Context $context = null)
113
    {
114
        if (null === $context) {
115
            return;
116
        }
117
118
        $statusCode = $context->attributes->get('status_code');
119
        if ($statusCode->isDefined()) {
120
            return $statusCode->get();
121
        }
122
    }
123
}
124