Test Failed
Push — master ( 38b4fb...3b71c1 )
by Marcus
01:34
created

EditUserForm::callbackSubmit()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 39
Code Lines 21

Duplication

Lines 39
Ratio 100 %

Code Coverage

Tests 17
CRAP Score 2.0276

Importance

Changes 0
Metric Value
dl 39
loc 39
ccs 17
cts 21
cp 0.8095
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 21
nc 2
nop 0
crap 2.0276
1
<?php
2
3
namespace Anax\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Anax\User\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, $acronym)
20
    {
21 2
        parent::__construct($di);
22
23 2
        $user = new User();
24 2
        $user->setDb($this->di->get("db"));
25
26
        // $acronym = $this->di->session->get("user");
27 2
        $user = $user->find("acronym", $acronym);
28
29 2
        $this->form->create(
30
            [
31 2
                "id" => __CLASS__,
32 2
                "legend" => "Redigera användare",
33 2
            ],
34
            [
35
                "id" => [
36 2
                    "value"       => $user->id,
37 2
                    "type"        => "hidden",
38 2
                ],
39
                "acronym" => [
40 2
                    "type"        => "text",
41 2
                    "value"       => $user->acronym,
42 2
                ],
43
44
                "password" => [
45 2
                    "type"        => "password",
46 2
                ],
47
48
                "password-again" => [
49 2
                    "type"        => "password",
50
                    "validation" => [
51
                        "match" => "password"
52 2
                    ],
53 2
                ],
54
                "email" => [
55 2
                    "type"        => "text",
56 2
                    "value"       => $user->email,
57 2
                ],
58
59
                "submit" => [
60 2
                    "type" => "submit",
61 2
                    "value" => "Skicka",
62 2
                    "callback" => [$this, "callbackSubmit"]
63 2
                ],
64
            ]
65 2
        );
66 2
    }
67
68
69
    /**
70
    * Callback for submit-button which should return true if it could
71
    * carry out its work and false if something failed.
72
    *
73
    * @return boolean true if okey, false if something went wrong.
74
    */
75 1 View Code Duplication
    public function callbackSubmit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
    // Get values from the submitted form
78 1
        $id            = $this->form->value("id");
79 1
        $acronym       = $this->form->value("acronym");
80 1
        $password      = $this->form->value("password");
81 1
        $passwordAgain = $this->form->value("password-again");
82 1
        $email         = $this->form->value("email");
83
84
        // Check password matches
85 1
        if ($password !== $passwordAgain) {
86
            $this->form->rememberValues();
87
            $this->form->addOutput("Password did not match.");
88
            return false;
89
        }
90
91
        // Save to database
92
        // $db = $this->di->get("db");
93
        // $password = password_hash($password, PASSWORD_DEFAULT);
94
        // $db->connect()
95
        //    ->insert("User", ["acronym", "password"])
96
        //    ->execute([$acronym, $password]);
97 1
        $user = new User();
98 1
        $user->setDb($this->di->get("db"));
99
100 1
        $user->acronym = $acronym;
101 1
        $user->email = $email;
102 1
        $user->id = $id;
103
104
        // create/update gravatar from email
105 1
        $gravatar = $user->gravatar($email);
106 1
        $user->gravatar = $gravatar;
107
108 1
        $user->setPassword($password);
109 1
        $user->save();
110
111
        $this->form->addOutput("Användarprofil uppdaterades.");
112 1
        return true;
113
    }
114
}
115