Completed
Push — symfony3-fqcn ( fc44dc...d46cdc )
by Kamil
34:08 queued 14:53
created

HttpFoundationRequestHandler::handleRequest()   C

Complexity

Conditions 15
Paths 15

Size

Total Lines 61
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 61
rs 6.2274
cc 15
eloc 34
nc 15
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
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 Sylius\Bundle\ResourceBundle\Form\Extension\HttpFoundation;
13
14
use Symfony\Component\Form\Exception\UnexpectedTypeException;
15
use Symfony\Component\Form\FormError;
16
use Symfony\Component\Form\FormInterface;
17
use Symfony\Component\Form\RequestHandlerInterface;
18
use Symfony\Component\Form\Util\ServerParams;
19
use Symfony\Component\HttpFoundation\Request;
20
21
/**
22
 * Does not compare the form's method with the request's method.
23
 * Always submits the form, even if there are no fields sent.
24
 *
25
 * @internal
26
 *
27
 * @see \Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler
28
 */
29
final class HttpFoundationRequestHandler implements RequestHandlerInterface
30
{
31
    /**
32
     * @var ServerParams
33
     */
34
    private $serverParams;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function __construct(ServerParams $serverParams = null)
40
    {
41
        $this->serverParams = $serverParams ?: new ServerParams();
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function handleRequest(FormInterface $form, $request = null)
48
    {
49
        if (!$request instanceof Request) {
50
            throw new UnexpectedTypeException($request, 'Symfony\Component\HttpFoundation\Request');
51
        }
52
53
        $name = $form->getName();
54
        $method = $request->getMethod();
55
56
        // For request methods that must not have a request body we fetch data
57
        // from the query string. Otherwise we look for data in the request body.
58
        if ('GET' === $method || 'HEAD' === $method || 'TRACE' === $method) {
59
            if ('' === $name) {
60
                $data = $request->query->all();
61
            } else {
62
                // Don't submit GET requests if the form's name does not exist
63
                // in the request
64
                if (!$request->query->has($name)) {
65
                    return;
66
                }
67
68
                $data = $request->query->get($name);
69
            }
70
        } else {
71
            // Mark the form with an error if the uploaded size was too large
72
            // This is done here and not in FormValidator because $_POST is
73
            // empty when that error occurs. Hence the form is never submitted.
74
            if ($this->serverParams->hasPostMaxSizeBeenExceeded()) {
75
                // Submit the form, but don't clear the default values
76
                $form->submit(null, false);
77
78
                $form->addError(new FormError(
79
                    call_user_func($form->getConfig()->getOption('upload_max_size_message')),
80
                    null,
81
                    array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())
82
                ));
83
84
                return;
85
            }
86
87
            if ('' === $name) {
88
                $params = $request->request->all();
89
                $files = $request->files->all();
90
            } elseif ($request->request->has($name) || $request->files->has($name)) {
91
                $default = $form->getConfig()->getCompound() ? array() : null;
92
                $params = $request->request->get($name, $default);
93
                $files = $request->files->get($name, $default);
94
            } else {
95
                // Don't submit the form if it is not present in the request
96
                return;
97
            }
98
99
            if (is_array($params) && is_array($files)) {
100
                $data = array_replace_recursive($params, $files);
101
            } else {
102
                $data = $params ?: $files;
103
            }
104
        }
105
106
        $form->submit($data, 'PATCH' !== $method);
107
    }
108
}
109