Feedback::onLoad()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
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
}