Feedback::onSubmitted()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
rs 8.5806
ccs 0
cts 17
cp 0
cc 4
eloc 17
nc 4
nop 1
crap 20
1
<?php 
2
3
namespace Helmut\Forms\Plugins\Feedback;
4
5
use Helmut\Forms\Plugin;
6
7
class Feedback extends Plugin {
8
9 134
    public function onLoad($form)
10
    {
11 134
        $form->addButton('feedback_trigger_'.$form->id);
12 134
    }
13
14
    public function onSubmitted($form) 
15
    {
16
        $request = $form->getRequest();
17
18
        if ($request->get('feedback_trigger_'.$form->id)) {
19
20
            $field = $form->field($request->get('name'));
21
22
            if ( ! is_null($field)) {
23
24
                $field->setFromRequest($request);
25
                
26
                $field->runValidations();
27
28
                $response = [
29
                    'id' => $field->id, 
30
                    'valid' => $field->valid(), 
31
                    'error_id' => 'feedback_errors_'.$field->id,
32
                    'error' => '', 
33
                ];
34
35
                if ( ! $field->valid()) {
36
                    $response['error'] = $form->renderFieldErrors($field);
37
                }
38
39
                header('Content-Type: application/json');
40
                echo json_encode($response);
41
                exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method onSubmitted() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
42
            }
43
        }
44
    }
45
46
}