AdminDeleteUserForm   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllItems() 0 12 2
A callbackSubmit() 0 8 1
A __construct() 0 23 1
1
<?php
2
3
namespace Radchasay\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Radchasay\User\User;
8
9
/**
10
 * Form to delete an item.
11
 */
12
class AdminDeleteUserForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19
    public function __construct(DIInterface $di)
20
    {
21
        parent::__construct($di);
22
        $this->form->create(
23
            [
24
                "id"     => __CLASS__,
25
                "legend" => "Delete an item",
26
            ],
27
            [
28
                "select" => [
29
                    "type"    => "select",
30
                    "label"   => "Select item to delete:",
31
                    "options" => $this->getAllItems(),
32
                ],
33
                
34
                "submit" => [
35
                    "type"     => "submit",
36
                    "value"    => "Delete item",
37
                    "callback" => [$this, "callbackSubmit"],
38
                ],
39
            ]
40
        );
41
    }
42
    
43
    
44
    /**
45
     * Get all items as array suitable for display in select option dropdown.
46
     *
47
     * @return array with key value of all items.
48
     */
49
    protected function getAllItems()
50
    {
51
        $user = new User();
52
        $user->setDb($this->di->get("db"));
53
        
54
        $users = ["-1" => "Select an item..."];
55
        foreach ($user->findAll() as $obj) {
56
            $users[ $obj->id ] = "{$obj->email} ({$obj->id})";
57
        }
58
        
59
        return $users;
60
    }
61
    
62
    
63
    /**
64
     * Callback for submit-button which should return true if it could
65
     * carry out its work and false if something failed.
66
     *
67
     * @return boolean true if okey, false if something went wrong.
68
     */
69
    public function callbackSubmit()
70
    {
71
        $user = new User();
72
        $user->setDb($this->di->get("db"));
73
        $user->find("id", $this->form->value("select"));
74
        $user->delete();
75
        $this->di->get("response")->redirect("admin/viewUsers");
76
    }
77
}
78