Completed
Push — symfony3-formsubmit ( 3f5146 )
by Kamil
78:09 queued 60:08
created

HttpFoundationRequestHandler   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 1
cbo 8
dl 0
loc 85
rs 10

2 Methods

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