diff options
author | Chris Cornutt <enygma@phpdeveloper.org> | 2015-06-12 16:41:22 -0500 |
---|---|---|
committer | Chris Cornutt <enygma@phpdeveloper.org> | 2015-06-12 16:41:22 -0500 |
commit | 2367ca577cd630ddf0be1014f081cee9a6cb1583 (patch) | |
tree | b8ff533d1074f69d2cbb9d6eb51b62290e0d5ed8 /src | |
parent | fc5939ebd757f021bda28379f7adda07bb7e2f17 (diff) | |
download | gatekeeper-2367ca577cd630ddf0be1014f081cee9a6cb1583.zip gatekeeper-2367ca577cd630ddf0be1014f081cee9a6cb1583.tar.gz gatekeeper-2367ca577cd630ddf0be1014f081cee9a6cb1583.tar.bz2 |
adding the "handlers" for the different kinds of requests made to Gatekeeper class (find, save, delete)
Diffstat (limited to 'src')
-rw-r--r-- | src/Psecio/Gatekeeper/Gatekeeper.php | 134 | ||||
-rw-r--r-- | src/Psecio/Gatekeeper/Handler.php | 105 | ||||
-rw-r--r-- | src/Psecio/Gatekeeper/Handler/Create.php | 30 | ||||
-rw-r--r-- | src/Psecio/Gatekeeper/Handler/Delete.php | 21 | ||||
-rw-r--r-- | src/Psecio/Gatekeeper/Handler/FindBy.php | 97 | ||||
-rw-r--r-- | src/Psecio/Gatekeeper/Handler/Save.php | 17 |
6 files changed, 276 insertions, 128 deletions
diff --git a/src/Psecio/Gatekeeper/Gatekeeper.php b/src/Psecio/Gatekeeper/Gatekeeper.php index c4ec7ba..1f86e9f 100644 --- a/src/Psecio/Gatekeeper/Gatekeeper.php +++ b/src/Psecio/Gatekeeper/Gatekeeper.php @@ -419,137 +419,15 @@ class Gatekeeper } if ($action == 'find') { - return self::handleFindBy($name, $args); + $action = new \Psecio\Gatekeeper\Handler\FindBy($name, $args, self::$datasource); } elseif ($action == 'create') { - return self::handleCreate($name, $args); + $action = new \Psecio\Gatekeeper\Handler\Create($name, $args, self::$datasource); } elseif ($action == 'delete') { - return self::handleDelete($name, $args); + $action = new \Psecio\Gatekeeper\Handler\Delete($name, $args, self::$datasource); } elseif ($action == 'save') { - return self::handleSave($name, $args); + $action = new \Psecio\Gatekeeper\Handler\Save($name, $args, self::$datasource); } - return false; - } - - /** - * Handle the "findBy" calls for data - * - * @param string $name Function name called - * @param array $args Arguments - * @throws \Exception\ModelNotFoundException If model type is not found - * @throws \Exception If Data could not be found - * @return object Model instance - */ - public static function handleFindBy($name, $args) - { - $action = 'find'; - $name = str_replace($action, '', $name); - preg_match('/By(.+)/', $name, $matches); - - if (empty($matches) && strtolower(substr($name, -1)) === 's') { - return self::handleFindByMultiple($name, $args, $matches); - } else { - return self::handleFindBySingle($name, $args, $matches); - } - - return $instance; - } - - /** - * Handle the "find by" when a single record is requested - * - * @param string $name Name of function called - * @param array $args Arguments list - * @param array $matches Matches from regex - * @return \Modler\Collection collection - */ - public static function handleFindBySingle($name, $args, $matches) - { - $property = lcfirst($matches[1]); - $model = str_replace($matches[0], '', $name); - $data = array($property => $args[0]); - - $modelNs = '\\Psecio\\Gatekeeper\\'.$model.'Model'; - if (!class_exists($modelNs)) { - throw new Exception\ModelNotFoundException('Model type '.$model.' could not be found'); - } - $instance = new $modelNs(self::$datasource); - $instance = self::$datasource->find($instance, $data); - - if ($instance->id === null) { - $exception = '\\Psecio\\Gatekeeper\\Exception\\'.$model.'NotFoundException'; - throw new $exception($model.' could not be found for criteria'); - } - - return $instance; - } - - /** - * Handle the "find by" when multiple are requested - * - * @param string $name Name of function called - * @param array $args Arguments list - * @param array $matches Matches from regex - * @return \Modler\Collection collection - */ - public static function handleFindByMultiple($name, $args, $matches) - { - $data = (isset($args[0])) ? $args[0] : array(); - $model = substr($name, 0, strlen($name) - 1); - $collectionNs = '\\Psecio\\Gatekeeper\\'.$model.'Collection'; - if (!class_exists($collectionNs)) { - throw new Exception\ModelNotFoundException('Collection type '.$model.' could not be found'); - } - $model = self::modelFactory($model.'Model'); - $collection = new $collectionNs(self::$datasource); - $collection = self::$datasource->find($model, $data, true); - - return $collection; - } - - /** - * Handle the calls to create a new instance - * - * @param string $name Function name - * @param array $args Argument set - * @throws Exception\ModelNotFoundException If model type is not found - * @return mixed Boolean false if method incorrect, model instance if created - */ - public static function handleCreate($name, array $args) - { - $model = '\\Psecio\\Gatekeeper\\'.str_replace('create', '', $name).'Model'; - if (class_exists($model) === true) { - $instance = new $model(self::$datasource, $args[0]); - $instance = self::$datasource->save($instance); - return $instance; - } else { - throw new Exception\ModelNotFoundException('Model type '.$model.' could not be found'); - } - return false; - } - - /** - * Handle the delete requests - * - * @param string $name Function name called - * @param array $args Arguments set - * @return boolean Success/fail of delete request - */ - public static function handleDelete($name, array $args) - { - $model = self::buildModel('delete', $name, $args); - return self::$datasource->delete($model); - } - - /** - * Handle the saving of a model instance - * - * @param string $name Name of funciton called - * @param array $args Arguments set - * @return boolean Success/fail of save request - */ - public static function handleSave($name, array $args) - { - return self::$datasource->save($args[0]); + return $action->execute(); } /** @@ -561,7 +439,7 @@ class Gatekeeper * @throws \Exception\ModelNotFoundException If model type is not found * @return object Model instance */ - protected static function buildModel($action = 'find', $name, array $args) + public static function buildModel($action = 'find', $name, array $args) { $name = str_replace($action, '', $name); preg_match('/By(.+)/', $name, $matches); diff --git a/src/Psecio/Gatekeeper/Handler.php b/src/Psecio/Gatekeeper/Handler.php new file mode 100644 index 0000000..18b43ab --- /dev/null +++ b/src/Psecio/Gatekeeper/Handler.php @@ -0,0 +1,105 @@ +<?php + +namespace Psecio\Gatekeeper; + +abstract class Handler +{ + /** + * Method name called for handler type + * @var string + */ + protected $name; + + /** + * Arguments called to pass into handler + * @var array + */ + protected $arguments = array(); + + /** + * Data source instance + * @var \Psecio\Gatekeeper\DataSource + */ + protected $datasource; + + /** + * Init the object and set up the name, arguments and data source + * + * @param string $name Method name called + * @param array $arguments Arguments to pass to handler + * @param \Psecio\Gatekeeper\DataSource $datasource Data source instance + */ + public function __construct($name, array $arguments, \Psecio\Gatekeeper\DataSource $datasource) + { + $this->setArguments($arguments); + $this->setName($name); + $this->setDb($datasource); + } + + /** + * Set the current arguments + * + * @param array $arguments Method arguments + */ + public function setArguments(array $arguments) + { + $this->arguments = $arguments; + } + + /** + * Get the current set of arguments + * + * @return array Arguemnt data set + */ + public function getArguments() + { + return $this->arguments; + } + + /** + * Set method name called for handler + * + * @param string $name Method name called + */ + public function setName($name) + { + $this->name = $name; + } + + /** + * Get the method name called + * + * @return string Method name + */ + public function getName() + { + return $this->name; + } + + /** + * Set the current data source + * + * @param \Psecio\Gatekeeper\DataSource $datasource data source instance (DB) + */ + public function setDb(\Psecio\Gatekeeper\DataSource $datasource) + { + $this->datasource = $datasource; + } + + /** + * Get the current data source instance + * + * @return \Psecio\Gatekeeper\DataSource instance + */ + public function getDb() + { + return $this->datasource; + } + + /** + * Execute the handler logic + * + * @return mixed + */ + abstract public function execute(); +}
\ No newline at end of file diff --git a/src/Psecio/Gatekeeper/Handler/Create.php b/src/Psecio/Gatekeeper/Handler/Create.php new file mode 100644 index 0000000..b715f1e --- /dev/null +++ b/src/Psecio/Gatekeeper/Handler/Create.php @@ -0,0 +1,30 @@ +<?php + +namespace Psecio\Gatekeeper\Handler; + +class Create extends \Psecio\Gatekeeper\Handler +{ + /** + * Execute the object/record creation handling + * + * @throws \Psecio\Gatekeeper\Exception\ModelNotFoundException If model type is not found + * @return mixed Either model object instance or false on failure + */ + public function execute() + { + $args = $this->getArguments(); + $name = $this->getName(); + + $model = '\\Psecio\\Gatekeeper\\'.str_replace('create', '', $name).'Model'; + if (class_exists($model) === true) { + $instance = new $model($this->getDb(), $args[0]); + $instance = $this->getDb()->save($instance); + return $instance; + } else { + throw new \Psecio\Gatekeeper\Exception\ModelNotFoundException( + 'Model type '.$model.' could not be found' + ); + } + return false; + } +}
\ No newline at end of file diff --git a/src/Psecio/Gatekeeper/Handler/Delete.php b/src/Psecio/Gatekeeper/Handler/Delete.php new file mode 100644 index 0000000..f0ca126 --- /dev/null +++ b/src/Psecio/Gatekeeper/Handler/Delete.php @@ -0,0 +1,21 @@ +<?php + +namespace Psecio\Gatekeeper\Handler; +use Psecio\Gatekeeper\Gatekeeper as g; + +class Delete extends \Psecio\Gatekeeper\Handler +{ + /** + * Execute the deletion handling + * + * @return boolean Success/failure of delete + */ + public function execute() + { + $args = $this->getArguments(); + $name = $this->getName(); + + $model = g::buildModel('delete', $name, $args); + return $this->getDb()->delete($model); + } +}
\ No newline at end of file diff --git a/src/Psecio/Gatekeeper/Handler/FindBy.php b/src/Psecio/Gatekeeper/Handler/FindBy.php new file mode 100644 index 0000000..67fda5b --- /dev/null +++ b/src/Psecio/Gatekeeper/Handler/FindBy.php @@ -0,0 +1,97 @@ +<?php + +namespace Psecio\Gatekeeper\Handler; + +class FindBy extends \Psecio\Gatekeeper\Handler +{ + /** + * Execute the "find by *" handling - smart enough to know + * if it's for one or multiple + * + * @return mixed Single model instance or collection on multiple + */ + public function execute() + { + $name = $this->getName(); + $args = $this->getArguments(); + + return $this->handleFindBy($name, $args); + } + + + /** + * Handle the "findBy" calls for data + * + * @param string $name Function name called + * @param array $args Arguments + * @throws \Exception\ModelNotFoundException If model type is not found + * @throws \Exception If Data could not be found + * @return object Model instance + */ + public function handleFindBy($name, $args) + { + $action = 'find'; + $name = str_replace($action, '', $name); + preg_match('/By(.+)/', $name, $matches); + + if (empty($matches) && strtolower(substr($name, -1)) === 's') { + return self::handleFindByMultiple($name, $args, $matches); + } else { + return self::handleFindBySingle($name, $args, $matches); + } + + return $instance; + } + + /** + * Handle the "find by" when a single record is requested + * + * @param string $name Name of function called + * @param array $args Arguments list + * @param array $matches Matches from regex + * @return \Modler\Collection collection + */ + public function handleFindBySingle($name, $args, $matches) + { + $property = lcfirst($matches[1]); + $model = str_replace($matches[0], '', $name); + $data = array($property => $args[0]); + + $modelNs = '\\Psecio\\Gatekeeper\\'.$model.'Model'; + if (!class_exists($modelNs)) { + throw new Exception\ModelNotFoundException('Model type '.$model.' could not be found'); + } + $instance = new $modelNs($this->getDb()); + $instance = $this->getDb()->find($instance, $data); + + if ($instance->id === null) { + $exception = '\\Psecio\\Gatekeeper\\Exception\\'.$model.'NotFoundException'; + throw new $exception($model.' could not be found for criteria'); + } + + return $instance; + } + + /** + * Handle the "find by" when multiple are requested + * + * @param string $name Name of function called + * @param array $args Arguments list + * @param array $matches Matches from regex + * @return \Modler\Collection collection + */ + public function handleFindByMultiple($name, $args, $matches) + { + $data = (isset($args[0])) ? $args[0] : array(); + $model = substr($name, 0, strlen($name) - 1); + $collectionNs = '\\Psecio\\Gatekeeper\\'.$model.'Collection'; + if (!class_exists($collectionNs)) { + throw new Exception\ModelNotFoundException('Collection type '.$model.' could not be found'); + } + $model = self::modelFactory($model.'Model'); + $collection = new $collectionNs($this->getDb()); + $collection = $this->getDb()->find($model, $data, true); + + return $collection; + } +}
\ No newline at end of file diff --git a/src/Psecio/Gatekeeper/Handler/Save.php b/src/Psecio/Gatekeeper/Handler/Save.php new file mode 100644 index 0000000..73938c2 --- /dev/null +++ b/src/Psecio/Gatekeeper/Handler/Save.php @@ -0,0 +1,17 @@ +<?php + +namespace Psecio\Gatekeeper\Handler; + +class Save extends \Psecio\Gatekeeper\Handler +{ + /** + * Execute the save handling + * + * @return boolean Success/fail result of save + */ + public function execute() + { + $args = $this->getArguments(); + return $this-getDb()->save($args[0]); + } +}
\ No newline at end of file |