EditProfileForm::__construct()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 18
cts 18
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Nicklas\Comment\HTMLForm\Profile;
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 EditProfileForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 3
    public function __construct(DIInterface $di, $name)
20
    {
21 3
        parent::__construct($di);
22 3
        $user = new User();
23 3
        $user->setDb($this->di->get("db"));
24 3
        $user->find("name", $name);
25 3
        $this->form->create(
26
            [
27 3
                "id" => __CLASS__,
28 3
                "class" => "login-widget",
29
                "fieldset" => true
30 3
            ],
31
            [
32
33
                "email" => [
34 3
                    "type"        => "text",
35 3
                    "value" => $user->email,
36 3
                ],
37
38
                "submit" => [
39 3
                    "type" => "submit",
40 3
                    "value" => "Update profile",
41 3
                    "callback" => [$this, "callbackSubmit"]
42 3
                ],
43
            ]
44 3
        );
45 3
    }
46
47
48
49
    /**
50
     * Callback for submit-button which should return true if it could
51
     * carry out its work and false if something failed.
52
     *
53
     * @return boolean true if okey, false if something went wrong.
54
     */
55 1
    public function callbackSubmit()
56
    {
57
        // Get values from the submitted form
58 1
        $email       = $this->form->value("email");
59
60 1
        if (strpos($email, '%') !== false) {
61
            $this->form->addOutput("% is not allowed");
62
            return false;
63
        }
64
65
66 1
        if (!$this->di->get('session')->get("user")) {
67 1
            return false;
68
        }
69
        $name = $this->di->get('session')->get("user");
70
          $user = new User();
71
          $user->setDb($this->di->get("db"));
72
          $user->find("name", $name);
73
74
          $user->email = $email;
75
          $user->save();
76
          return true;
77
    }
78
}
79