summaryrefslogtreecommitdiffstats
path: root/source/Command/User/Add.php
blob: 088feefdb8dad904122ebbfcdc3d53727eaab435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since 2014-08-12
 */

/**
 * Class Command_User_Add
 */
class Command_User_Add extends Command_User_AbstractCommand
{
    /**
     * @var array
     */
    private $inputChannels;

    /**
     * @var string
     */
    private $inputName;

    /**
     * @var string
     */
    private $inputPassword;

    /**
     * @var string
     */
    private $inputRole;

    /**
     * @throws Exception
     */
    public function execute()
    {
        end($this->users);
        $nextKey = (key($this->users) + 1);
        reset($this->users);

        $content = $this->file->read();

        $content[] = '// added - ' . date('Y-m-d H:i:s');
        $content[] = '$users[' . $nextKey . '] = array();';
        $content[] = '$users[' . $nextKey . '][\'userRole\'] = ' . $this->roles[$this->inputRole] . ';';
        $content[] = '$users[' . $nextKey . '][\'userName\'] = \'' . $this->inputName . '\';';
        $content[] = '$users[' . $nextKey . '][\'password\'] = \'' . $this->inputPassword . '\';';
        $content[] = '$users[' . $nextKey . '][\'channels\'] = array(' . implode(',', $this->inputChannels) . ');';

        $this->file->write($content);
    }

    /**
     * @return array
     */
    public function getUsage()
    {
        return array(
            'name="<name>" password="<password>" role=<id> channels="<id>[,<id>[...]]"',
            '   available channels: ' . implode(',', array_keys($this->channels)),
            '   available roles: ' . implode(',', array_keys($this->roles))
        );
    }

    /**
     * @throws Exception
     */
    public function verify()
    {
        if ($this->input->getNumberOfArguments() !== 4) {
            throw new Exception(
                'invalid number of arguments provided'
            );
        }

        $channels = explode(',', $this->input->getParameterValue('channels', ''));
        $name = $this->input->getParameterValue('name');
        $password = $this->input->getParameterValue('password');
        $role = $this->input->getParameterValue('role');

        if (is_null($name)) {
            throw new Exception(
                'no name "' . $name . '" provided'
            );
        }

        if (is_null($role)) {
            throw new Exception(
                'no role "' . $role . '" provided'
            );
        } else {
            if (!isset($this->roles[$role])) {
                throw new Exception(
                    'invalid role "' . $role . '" provided'
                );
            }
        }

        if (is_null($password)) {
            throw new Exception(
                'no password "' . $password . '" provided'
            );
        }

        if (empty($channels)) {
            throw new Exception(
                'no channels provided'
            );
        }

        foreach ($channels as $channel) {
            if (!isset($this->channels[$channel])) {
                throw new Exception(
                    'invalid channel "' . $channel . '" provided'
                );
            }
        }

        $this->inputChannels = $channels;
        $this->inputName = $name;
        $this->inputPassword = $password;
        $this->inputRole = $role;
    }
}