summaryrefslogtreecommitdiffstats
path: root/source/Command/User/Edit.php
diff options
context:
space:
mode:
authorPhilip Nicolcev <frugen@gmail.com>2014-08-24 13:12:44 -0400
committerPhilip Nicolcev <frugen@gmail.com>2014-08-24 13:12:44 -0400
commitf5fe739314de24e3a9cb94b9c8c64e11ed9cc14c (patch)
treebe46c6120d6dc0d08a7112a8dc80840281cbad33 /source/Command/User/Edit.php
parent9ffeed7646620ae7de51b929a3b44f2cdffb677f (diff)
parent5d8755faf06fd2ff9d3977f103e641b0ba9f714e (diff)
downloadAJAX-Chat-origin/cli_testing.zip
AJAX-Chat-origin/cli_testing.tar.gz
AJAX-Chat-origin/cli_testing.tar.bz2
Merge pull request #178 from stevleibelt/masterorigin/cli_testing
Implemented Command Line Tools in cli_testing branch
Diffstat (limited to 'source/Command/User/Edit.php')
-rw-r--r--source/Command/User/Edit.php161
1 files changed, 161 insertions, 0 deletions
diff --git a/source/Command/User/Edit.php b/source/Command/User/Edit.php
new file mode 100644
index 0000000..72467ee
--- /dev/null
+++ b/source/Command/User/Edit.php
@@ -0,0 +1,161 @@
+<?php
+/**
+ * @author stev leibelt <artodeto@bazzline.net>
+ * @since 2014-08-13
+ */
+
+/**
+ * Class Command_User_Edit
+ */
+class Command_User_Edit extends Command_User_AbstractCommand
+{
+ /**
+ * @var array
+ */
+ private $inputChannels;
+
+ /**
+ * @var string
+ */
+ private $inputName;
+
+ /**
+ * @var string
+ */
+ private $inputPassword;
+
+ /**
+ * @var string
+ */
+ private $inputRole;
+
+ /**
+ * @var int
+ */
+ private $inputId;
+
+ /**
+ * @throws Exception
+ */
+ public function execute()
+ {
+ reset($this->users);
+
+ $lines = $this->file->read();
+ $content = array();
+ $contentAfterCurrentUser = array();
+ $string = new String();
+
+ $foundCurrentUserContent = false;
+ $linePrefixToSearchFor = '$users[' . $this->inputId . '][';
+
+ foreach ($lines as $line) {
+ if ($string->startsWith($line, $linePrefixToSearchFor)) {
+ $foundCurrentUserContent = true;
+ } else {
+ if ($foundCurrentUserContent) {
+ $contentAfterCurrentUser[] = $line;
+ } else {
+ $content[] = $line;
+ }
+ }
+ }
+
+ $content[] = '$users[' . $this->inputId . '][\'userRole\'] = ' . $this->roles[$this->inputRole] . ';';
+ $content[] = '$users[' . $this->inputId . '][\'userName\'] = \'' . $this->inputName . '\';';
+ $content[] = '$users[' . $this->inputId . '][\'password\'] = \'' . $this->inputPassword . '\';';
+ $content[] = '$users[' . $this->inputId . '][\'channels\'] = array(' . implode(',', $this->inputChannels) . ');';
+
+ foreach ($contentAfterCurrentUser as $line) {
+ $content[] = $line;
+ }
+
+ $this->file->write($content);
+ }
+
+ /**
+ * @return array
+ */
+ public function getUsage()
+ {
+ return array(
+ 'user_id=<id> name="<name>" password="<password>" role=<id> channels="<id>[,<id>[...]]"',
+ ' available channels: ' . implode(',', array_keys($this->channels)),
+ ' available roles: ' . implode(',', array_keys($this->roles)),
+ ' available users: ' . implode(',', array_keys($this->users))
+ );
+ }
+
+ /**
+ * @throws Exception
+ */
+ public function verify()
+ {
+ if ($this->input->getNumberOfArguments() !== 5) {
+ 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');
+ $userId = $this->input->getParameterValue('user_id');
+ $validIds = array_keys($this->users);
+
+ if (is_null($name)) {
+ throw new Exception(
+ 'no name provided'
+ );
+ }
+
+ if (is_null($role)) {
+ throw new Exception(
+ 'no role provided'
+ );
+ } else {
+ if (!isset($this->roles[$role])) {
+ throw new Exception(
+ 'invalid role "' . $role . '" provided'
+ );
+ }
+ }
+
+ if (is_null($password)) {
+ throw new Exception(
+ 'no 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'
+ );
+ }
+ }
+
+ if (($userId === 0)) {
+ throw new Exception(
+ 'no id provided'
+ );
+ } else if (!isset($validIds[$userId])) {
+ throw new Exception(
+ 'invalid id provided'
+ );
+ }
+
+ $this->inputChannels = $channels;
+ $this->inputName = $name;
+ $this->inputPassword = $password;
+ $this->inputRole = $role;
+ $this->inputId = $userId;
+ }
+} \ No newline at end of file