I really need a review on the structure, and how things are done. I want to improve.
Command.class:
<?php
/**
* Command
* Processing commands to the server, acting, receiving response.
*
* @author Jony <[email protected]>
*/
class Command extends PrepareCommand
{
/**
* Client
* The command processor
* @object Client
*/
private $client;
/**
* Annonymous constructor
* Constructs the application.
*
* @param message ; Object Client
*/
public function __construct(Client $message)
{
$this->client = $message;
if ($this->isUsingCommand())
{
$this->_prepare();
}
}
/**
* Protected _prepare
* Preparing the command line.
*
* @return void
*/
protected function _prepare()
{
$this->_commander = $this->client;
$this->_process();
$this->processCommand();
}
/**
* processCommand
* Processing the given commmand, finds out
* what command was used, and it's data.
*
* @var _commandLine ; The used command name.
* @var _commandActions ; The data that comes after the command
* @return void
*/
private function processCommand()
{
switch ($this->_commandLine[1])
{
case "ban":
echo "LOl you tried to ban " . $this->fullString();
break;
case "yell":
echo $this->fullString();
break;
}
}
/**
* isUsingCommand
* Is the Client using a command at all?
* Checks if line starts with the command sign " / ".
*
* @return boolean
*/
public function isUsingCommand()
{
return (substr($this->client->getMessage(), 0, 1) === "/") ? true : false;
}
/**
* fullString
* Gets all of the array elements of _commandActions and
* converts it to one big String
*
* @var message ; The String message.
* @var _commandActions ; The data that comes after the command.
* @return String
*/
private function fullString()
{
$message = "";
for ($i = 1; $i < count($this->_commandActions); $i++)
{
$message .= " " . $this->_commandActions[$i];
}
return $message;
}
}
?>
PrepareCommand.class:
<?php
abstract class PrepareCommand
{
/**
* Properties
*/
private $command;
protected $_commander;
protected $_commandActions;
protected $_commandLine;
/**
* Processing the command, and calling parse
* to explore the commandline.
*
* @return void
*/
protected function _process()
{
$this->command = $this->_commander->getMessage();
$this->parse();
}
/**
* Parsing the command ; Exploring it ;
* Getting the first command and actions.
*
* @var _commandActions ; Holding all of the words, between the entered spaces.
* @var _commandLine ; Getting the entered command name after the " / " sign.
* @return void
*/
private function parse()
{
$this->_commandActions = explode(" ", $this->command);
$this->_commandLine = explode("/", $this->_commandActions[0]);
}
/**
* Abstract prapre
*
* Activating the class, and preparing the command
* through the class.
*
* @return void
*/
protected abstract function _prepare();
}
?>
usage:
new Command(new Client($_POST['process'], date('time'), "guest"));