Test Failed
Push — master ( 7c8781...44e581 )
by Nicklas
02:28
created

EditUserForm::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1.5668

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 5
cts 29
cp 0.1724
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 26
nc 1
nop 2
crap 1.5668
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 1
    public function __construct(DIInterface $di, $id)
20
    {
21 1
        parent::__construct($di);
22 1
        $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 1
        $this->user->setDb($this->di->get("db"));
24 1
        $this->user->find("id", $id);
25
        $user = $this->user;
26
        $this->form->create(
27
            [
28
                "id" => __CLASS__,
29
                "fieldset" => true,
30
                "legend" => "Update user: $user->name"
31
            ],
32
            [
33
34
                "name" => [
35
                    "type"        => "text",
36
                    "readonly" => true,
37
                    "value" => $user->name,
38
                ],
39
                "email" => [
40
                    "type"        => "text",
41
                    "value" => $user->email,
42
                ],
43
                "select" => [
44
                    "type"        => "select",
45
                    "label"       => "Select authority",
46
                    "options"     => ["$user->authority" => $user->authority, "admin" => "admin", "user" => "user"],
47
                ],
48
                "submit" => [
49
                    "type" => "submit",
50
                    "value" => "Update",
51
                    "onclick"=>"return confirm('Fick du verkligen allt rätt?');",
52
                    "callback" => [$this, "callbackSubmit"]
53
                ],
54
            ]
55
        );
56
    }
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