diff options
Diffstat (limited to 'source/Command/Channel')
-rw-r--r-- | source/Command/Channel/AbstractCommand.php | 37 | ||||
-rw-r--r-- | source/Command/Channel/Add.php | 66 | ||||
-rw-r--r-- | source/Command/Channel/CommandInterface.php | 21 | ||||
-rw-r--r-- | source/Command/Channel/Delete.php | 87 | ||||
-rw-r--r-- | source/Command/Channel/Edit.php | 102 | ||||
-rw-r--r-- | source/Command/Channel/List.php | 54 |
6 files changed, 367 insertions, 0 deletions
diff --git a/source/Command/Channel/AbstractCommand.php b/source/Command/Channel/AbstractCommand.php new file mode 100644 index 0000000..42b6836 --- /dev/null +++ b/source/Command/Channel/AbstractCommand.php @@ -0,0 +1,37 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Class Command_Channel_AbstractCommand + */ +abstract class Command_Channel_AbstractCommand extends Command_AbstractCommand implements Command_Channel_CommandInterface +{ + /** + * @var File + */ + protected $file; + + /** + * @var array + */ + protected $channels; + + /** + * @param array $channels + */ + public function setChannels(array $channels) + { + $this->channels = $channels; + } + + /** + * @param File $file + */ + public function setChannelFile(File $file) + { + $this->file = $file; + } +}
\ No newline at end of file diff --git a/source/Command/Channel/Add.php b/source/Command/Channel/Add.php new file mode 100644 index 0000000..751970c --- /dev/null +++ b/source/Command/Channel/Add.php @@ -0,0 +1,66 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Class Command_Channel_Add + */ +class Command_Channel_Add extends Command_Channel_AbstractCommand +{ + /** + * @var string + */ + private $inputName; + + /** + * @throws Exception + */ + public function execute() + { + end($this->channels); + $nextKey = (key($this->channels) + 1); + reset($this->channels); + + $content = $this->file->read(); + + $content[] = '// added - ' . date('Y-m-d H:i:s'); + $content[] = '$channels[' . $nextKey . '] = \'' . $this->inputName . '\';'; + + $this->file->write($content); + } + + /** + * @return array + */ + public function getUsage() + { + return array( + 'name="<name>"', + ' available channels: ' . implode(',', array_keys($this->channels)), + ); + } + + /** + * @throws Exception + */ + public function verify() + { + if ($this->input->getNumberOfArguments() !== 1) { + throw new Exception( + 'invalid number of arguments provided' + ); + } + + $name = $this->input->getParameterValue('name'); + + if (is_null($name)) { + throw new Exception( + 'invalid name "' . $name . '" provided' + ); + } + + $this->inputName = $name; + } +} diff --git a/source/Command/Channel/CommandInterface.php b/source/Command/Channel/CommandInterface.php new file mode 100644 index 0000000..72af707 --- /dev/null +++ b/source/Command/Channel/CommandInterface.php @@ -0,0 +1,21 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Interface Command_Channel_CommandInterface + */ +interface Command_Channel_CommandInterface extends Command_CommandInterface +{ + /** + * @param array $channels + */ + public function setChannels(array $channels); + + /** + * @param File $file + */ + public function setChannelFile(File $file); +}
\ No newline at end of file diff --git a/source/Command/Channel/Delete.php b/source/Command/Channel/Delete.php new file mode 100644 index 0000000..af7a791 --- /dev/null +++ b/source/Command/Channel/Delete.php @@ -0,0 +1,87 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Class Command_Channel_Delete + */ +class Command_Channel_Delete extends Command_Channel_AbstractCommand +{ + /** + * @var int + */ + private $inputId; + + /** + * @throws Exception + */ + public function execute() + { + reset($this->channels); + + $lines = $this->file->read(); + $content = array(); + + foreach ($lines as $line) { + if ($line == '// Sample channel list:') { + $content[] = $line; + break; + } else { + $content[] = $line; + } + } + + if (empty($this->channels)) { + throw new Exception( + 'nothing to delete' + ); + } else { + unset($this->channels[$this->inputId]); + $ids = array_values($this->channels); + + if (!empty($ids)) { + foreach ($ids as $id => $name) { + $content[] = '$channels[' . $id . '] = \'' . $name . '\';'; + } + } + + $this->file->write($content); + } + } + + /** + * @return array + */ + public function getUsage() + { + return array( + 'channel_id=<channel id>', + ' available channels: ' . implode(',', array_keys($this->channels)) + ); + } + + /** + * @throws Exception + */ + public function verify() + { + if ($this->input->getNumberOfArguments() !== 1) { + throw new Exception( + 'invalid number of arguments provided' + ); + } + + $validIds = array_keys($this->channels); + $inputId = $this->input->getParameterValue('channel_id'); + + if (!isset($validIds[$inputId])) { + throw new Exception( + 'invalid name "' . $inputId . '" provided' + ); + } + + $this->inputId = $inputId; + } +} diff --git a/source/Command/Channel/Edit.php b/source/Command/Channel/Edit.php new file mode 100644 index 0000000..fc852b6 --- /dev/null +++ b/source/Command/Channel/Edit.php @@ -0,0 +1,102 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Class Command_Channel_Edit + */ +class Command_Channel_Edit extends Command_Channel_AbstractCommand +{ + /** + * @var string + */ + private $inputName; + + /** + * @var int + */ + private $inputId; + + /** + * @throws Exception + */ + public function execute() + { + reset($this->channels); + + $lines = $this->file->read(); + $content = array(); + $contentAfterCurrentChannel = array(); + $string = new String(); + + $foundCurrentChannelLine = false; + $linePrefixToSearchFor = '$channels[' . $this->inputId . ']'; + + foreach ($lines as $line) { + if ($string->startsWith($line, $linePrefixToSearchFor)) { + $foundCurrentChannelLine = true; + } else { + if ($foundCurrentChannelLine) { + $contentAfterCurrentChannel[] = $line; + } else { + $content[] = $line; + } + } + } + + $content[] = '$channels[' . $this->inputId . '] = \'' . $this->inputName . '\';'; + + foreach ($contentAfterCurrentChannel as $line) { + $content[] = $line; + } + + $this->file->write($content); + } + + /** + * @return array + */ + public function getUsage() + { + return array( + 'id=<id> name="<name>"', + ' available channels: ' . implode(',', array_keys($this->channels)) + ); + } + + /** + * @throws Exception + */ + public function verify() + { + if ($this->input->getNumberOfArguments() !== 2) { + throw new Exception( + 'invalid number of arguments provided' + ); + } + + $name = $this->input->getParameterValue('name'); + $id = $this->input->getParameterValue('id'); + + if (is_null($name)) { + throw new Exception( + 'no name provided' + ); + } + + if (is_null($id)) { + throw new Exception( + 'no id provided' + ); + } else if (!isset($this->channels[$id])) { + throw new Exception( + 'invalid id provided' + ); + } + + $this->inputName = $name; + $this->inputId = $id; + } +}
\ No newline at end of file diff --git a/source/Command/Channel/List.php b/source/Command/Channel/List.php new file mode 100644 index 0000000..caaa445 --- /dev/null +++ b/source/Command/Channel/List.php @@ -0,0 +1,54 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Class Command_Channel_List + */ +class Command_Channel_List extends Command_Channel_AbstractCommand +{ + /** + * @throws Exception + */ + public function execute() + { + $numberOfChannels = count($this->channels); + + $this->output->addLine('number of channels: ' . $numberOfChannels); + + if ($numberOfChannels > 0) { + $this->output->addLine(); + $this->output->addLine('id | name '); + $this->output->addLine('--------'); + + foreach ($this->channels as $id => $name) { + $this->output->addLine( + implode( + ' | ', + array( + $id, + $name + ) + ) + ); + } + } + } + + /** + * @return array + */ + public function getUsage() + { + return array(); + } + + /** + * @throws Exception + */ + public function verify() + { + } +} |