EditUserForm::callbackSubmit()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 0
crap 12
1
<?php
2
3
namespace Nicklas\Comment\HTMLForm\Admin;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Nicklas\Comment\Modules\User;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class EditUserForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 2
    public function __construct(DIInterface $di, $id)
20
    {
21 2
        parent::__construct($di);
22 2
        $this->user = new User();
0 ignored issues
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23 2
        $this->user->setDb($this->di->get("db"));
24 2
        $this->user->find("id", $id);
25 2
        $user = $this->user;
26 2
        $this->form->create(
27
            [
28 2
                "id" => __CLASS__,
29 2
                "fieldset" => true,
30 2
                "legend" => "Update user: $user->name"
31 2
            ],
32
            [
33
34
                "name" => [
35 2
                    "type"        => "text",
36 2
                    "readonly" => true,
37 2
                    "value" => $user->name,
38 2
                ],
39
                "email" => [
40 2
                    "type"        => "text",
41 2
                    "value" => $user->email,
42 2
                ],
43
                "select" => [
44 2
                    "type"        => "select",
45 2
                    "label"       => "Select authority",
46 2
                    "options"     => ["$user->authority" => $user->authority, "admin" => "admin", "user" => "user"],
47 2
                ],
48
                "submit" => [
49 2
                    "type" => "submit",
50 2
                    "value" => "Update",
51 2
                    "onclick"=>"return confirm('Fick du verkligen allt rätt?');",
52 2
                    "callback" => [$this, "callbackSubmit"]
53 2
                ],
54
            ]
55 2
        );
56 2
    }
57
58
59
    /**
60
     * Callback for submit-button which should return true if it could
61
     * carry out its work and false if something failed.
62
     *
63
     * @return boolean true if okey, false if something went wrong.
64
     */
65
    public function callbackSubmit()
66
    {
67
        // Get values from the submitted form
68
        $email       = $this->form->value("email");
69
70
        if (strpos($email, '%') !== false) {
71
            $this->form->addOutput("% is not allowed");
72
            return false;
73
        }
74
75
          $user = $this->user;
76
77
          $user->email = $email;
78
          $user->authority = $this->form->value("select") ?: "user";
79
          $user->save();
80
          $this->form->addOutput("Du uppdaterade användaren");
81
          return true;
82
    }
83
}
84