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

EditProfileForm   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 15.63%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 67
ccs 5
cts 32
cp 0.1563
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 27 1
A callbackSubmit() 0 23 3
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 1
    public function __construct(DIInterface $di, $name)
20
    {
21 1
        parent::__construct($di);
22 1
        $user = new User();
23 1
        $user->setDb($this->di->get("db"));
24 1
        $user->find("name", $name);
25
        $this->form->create(
26
            [
27
                "id" => __CLASS__,
28
                "class" => "login-widget",
29
                "fieldset" => true
30
            ],
31
            [
32
33
                "email" => [
34
                    "type"        => "text",
35
                    "value" => $user->email,
36
                ],
37
38
                "submit" => [
39
                    "type" => "submit",
40
                    "value" => "Update profile",
41
                    "callback" => [$this, "callbackSubmit"]
42
                ],
43
            ]
44
        );
45
    }
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
    public function callbackSubmit()
56
    {
57
        // Get values from the submitted form
58
        $email       = $this->form->value("email");
59
60
        if (strpos($email, '%') !== false) {
61
            $this->form->addOutput("% is not allowed");
62
            return false;
63
        }
64
65
66
        if (!$this->di->get('session')->get("user")) {
67
            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