Completed
Push — symfony3 ( ec9673...e0eba1 )
by Kamil
18:54
created

HttpFoundationRequestHandler::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
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
 * A request processor using the {@link Request} class of the HttpFoundation
23
 * component.
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class HttpFoundationRequestHandler implements RequestHandlerInterface
28
{
29
    /**
30
     * @var ServerParams
31
     */
32
    private $serverParams;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function __construct(ServerParams $serverParams = null)
38
    {
39
        $this->serverParams = $serverParams ?: new ServerParams();
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function handleRequest(FormInterface $form, $request = null)
46
    {
47
        if (!$request instanceof Request) {
48
            throw new UnexpectedTypeException($request, 'Symfony\Component\HttpFoundation\Request');
49
        }
50
51
        $name = $form->getName();
52
        $method = $request->getMethod();
53
54
        // For request methods that must not have a request body we fetch data
55
        // from the query string. Otherwise we look for data in the request body.
56
        if ('GET' === $method || 'HEAD' === $method || 'TRACE' === $method) {
57
            if ('' === $name) {
58
                $data = $request->query->all();
59
            } else {
60
                // Don't submit GET requests if the form's name does not exist
61
                // in the request
62
                if (!$request->query->has($name)) {
63
                    return;
64
                }
65
66
                $data = $request->query->get($name);
67
            }
68
        } else {
69
            // Mark the form with an error if the uploaded size was too large
70
            // This is done here and not in FormValidator because $_POST is
71
            // empty when that error occurs. Hence the form is never submitted.
72
            if ($this->serverParams->hasPostMaxSizeBeenExceeded()) {
73
                // Submit the form, but don't clear the default values
74
                $form->submit(null, false);
75
76
                $form->addError(new FormError(
77
                    call_user_func($form->getConfig()->getOption('upload_max_size_message')),
78
                    null,
79
                    array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())
80
                ));
81
82
                return;
83
            }
84
85
            if ('' === $name) {
86
                $params = $request->request->all();
87
                $files = $request->files->all();
88
            } elseif ($request->request->has($name) || $request->files->has($name)) {
89
                $default = $form->getConfig()->getCompound() ? array() : null;
90
                $params = $request->request->get($name, $default);
91
                $files = $request->files->get($name, $default);
92
            } else {
93
                // Don't submit the form if it is not present in the request
94
                return;
95
            }
96
97
            if (is_array($params) && is_array($files)) {
98
                $data = array_replace_recursive($params, $files);
99
            } else {
100
                $data = $params ?: $files;
101
            }
102
        }
103
104
        // Don't auto-submit the form unless at least one field is present.
105
        if ('' === $name && count(array_intersect_key($data, $form->all())) <= 0) {
106
            return;
107
        }
108
109
        $form->submit($data, 'PATCH' !== $method);
110
    }
111
}
112