Container::handleRequest()   C
last analyzed

Complexity

Conditions 13
Paths 8

Size

Total Lines 49
Code Lines 25

Duplication

Lines 10
Ratio 20.41 %

Importance

Changes 0
Metric Value
dl 10
loc 49
rs 5.1401
c 0
b 0
f 0
cc 13
eloc 25
nc 8
nop 1

How to fix   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
 * Container of Form lib
5
 *
6
 * @category  	Venus
7
 * @package		Venus\lib\Form
8
 * @author    	Judicaël Paquet <[email protected]>
9
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
10
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
11
 * @version   	Release: 1.0.0
12
 * @filesource	https://github.com/las93/venus2
13
 * @link      	https://github.com/las93
14
 * @since     	1.0
15
 */
16
namespace Venus\lib\Form;
17
18
use \Attila\lib\Entity  as LibEntity;
19
use \Venus\lib\Form     as Form;
20
21
/**
22
 * Container of Form lib
23
 *
24
 * @category  	Venus
25
 * @package		Venus\lib\Form
26
 * @author    	Judicaël Paquet <[email protected]>
27
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
28
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
29
 * @version   	Release: 1.0.0
30
 * @filesource	https://github.com/las93/venus2
31
 * @link      	https://github.com/las93
32
 * @since     	1.0
33
 */
34
class Container
35
{
36
    /**
37
     * complete view
38
     *
39
     * @access private
40
     * @var    string
41
     */
42
    private $_sView = null;
43
44
    /**
45
     * form library with its entity
46
     *
47
     * @access private
48
     * @var    Form
49
     */
50
    private $_oForm = null;
51
52
    /**
53
     * Block the save in the entity if you don't call handleRequest
54
     *
55
     * @access private
56
     * @var    bool
57
     */
58
    private $_bHandleRequestActivate = false;
59
60
    /**
61
     * Request of the formular
62
     *
63
     * @access private
64
     * @var    array
65
     */
66
    private $_aRequest = null;
67
68
    /**
69
     * get the Value
70
     *
71
     * @access public
72
     * @return \stdClass
73
     */
74
    public function createView() : \stdClass
75
    {
76
        $oView = new \stdClass;
77
        $oView->form = $this->_sView;
78
        $oView->form_start = $this->_oForm->getFormInObject()->start;
79
        $oView->form_end = $this->_oForm->getFormInObject()->end;
80
        $oView->form_row = array();
81
82
        foreach ($this->_oForm->getFormInObject()->form as $sKey => $mValue) {
83
84
            if ($mValue instanceof Container) {
85
86
                $oNewForm = $mValue->createView();
87
                $oView->form_row[$sKey] = $oNewForm->form_row;
88
            } else {
89
90
               $oView->form_row[$sKey] = $mValue;
91
            }
92
        }
93
94
        return $oView;
95
    }
96
97
    /**
98
     * set the Value
99
     *
100
     * @access public
101
     * @param  string $sView Display of form;
102
     * @return \Venus\lib\Form\Container
103
     */
104
    public function setView(string $sView) : Container
105
    {
106
        $this->_sView = $sView;
107
        return $this;
108
    }
109
110
    /**
111
     * handle the request to do many actions on it
112
     *
113
     * @access public
114
     * @param  array $aRequest request like $_POST
115
     * @return bool
116
     */
117
    public function handleRequest(array $aRequest) : bool
118
    {
119
        if (!count($_POST)) { return true; }
120
121
        // Validation
122
        foreach ($this->_oForm->getElement() as $sKey => $sValue) {
123
124
            if (!$sValue instanceof self && !$this->_validate($sValue)) {
125
126
                return false;
127
            }
128
        }
129
130
        // Save
131
        if ($this->_oForm->getIdEntity() > 0 && $this->_oForm->getSynchronizeEntity() !== null && count($aRequest) > 0) {
132
133
            $sModelName = str_replace('Entity', 'Model', $this->_oForm->getSynchronizeEntity());
134
            $oModel = new $sModelName;
0 ignored issues
show
Unused Code introduced by
$oModel is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
135
136
            $oEntity = new $this->_oForm->getSynchronizeEntity();
0 ignored issues
show
Bug introduced by
The property getSynchronizeEntity does not seem to exist in Venus\lib\Form.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
137
            $sPrimaryKey = LibEntity::getPrimaryKeyNameWithoutMapping($oEntity);
138
            $sMethodName = 'set_'.$sPrimaryKey;
139
140
            call_user_func_array(array(&$oEntity, $sMethodName), array($this->_oForm->getIdEntity()));
141
142 View Code Duplication
            foreach ($this->_oForm->getElement() as $sKey => $sValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
143
144
                $sMethodName = 'set_'.$sValue->getName().'';
145
                call_user_func_array(array(&$oEntity, $sMethodName), array($aRequest[$sValue->getName()]));
146
            }
147
148
            $oEntity->save();
149
        } else if ($this->_oForm->getSynchronizeEntity() !== null && isset($aRequest) && count($aRequest) > 0) {
150
151
            $oEntity = new $this->_oForm->_sSynchronizeEntity;
0 ignored issues
show
Bug introduced by
The property _sSynchronizeEntity cannot be accessed from this context as it is declared private in class Venus\lib\Form.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
152
153 View Code Duplication
            foreach ($this->_oForm->getElement() as $sKey => $sValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
154
155
                $sMethodName = 'set_'.$sValue->getName().'';
156
                call_user_func_array(array(&$oEntity, $sMethodName), array($aRequest[$sValue->getName()]));
157
            }
158
159
            $this->_oForm->setIdEntityCreated($oEntity->save());
160
        }
161
162
        $this->_bHandleRequestActivate = true;
163
        $this->_aRequest = $aRequest;
164
        return true;
165
    }
166
167
    /**
168
     * set Form lib with its entity
169
     *
170
     * @access public
171
     * @param  Form $oForm request like $_POST
172
     * @return \Venus\lib\Form\Container
173
     */
174
    public function setForm(Form $oForm) : Container
175
    {
176
        $this->_oForm = $oForm;
177
        return $this;
178
    }
179
180
    /**
181
     * if this form is validate and save
182
     *
183
     * @access public
184
     * @return boolean
185
     */
186
    public function isValid() : bool
187
    {
188
        if ($this->_bHandleRequestActivate === true) { return true; } else { return false; }
189
    }
190
191
    /**
192
     * if this form is validate and save
193
     *
194
     * @access public
195
     * @return boolean
196
     */
197
    public function isSubmitted() : bool
198
    {
199
        if (isset($_POST['validform'.$this->_oForm->getFormNumber()]) && $_POST['validform'.$this->_oForm->getFormNumber()] == 1) {
200
201
            return true;
202
        } else {
203
204
            return false;
205
        }
206
    }
207
208
    /**
209
     * if this form is validate and save
210
     *
211
     * @access public
212
     * @param  string $sElementName element name what we want test the click
213
     * @return boolean
214
     */
215
    public function isClicked(string $sElementName) : bool
216
    {
217
        if (isset($_POST[$sElementName]) && $_POST[$sElementName]) { return true; } else { return false; }
218
    }
219
220
    /**
221
     * if the element is valide or not (with the constraint)
222
     *
223
     * @access private
224
     * @param  object $oElement element of formular
225
     * @return boolean
226
     */
227
    private function _validate($oElement) : bool
228
    {
229
        foreach ($oElement->getConstraint() as $oConstraint) {
230
231
            if (!$oConstraint->validate($_POST[$oElement->getName()])) {
232
233
                return false;
234
            }
235
        }
236
237
        return true;
238
    }
239
}
240