diff options
author | Chris Cornutt <chris.cornutt@hp.com> | 2014-12-16 11:31:32 -0600 |
---|---|---|
committer | Chris Cornutt <chris.cornutt@hp.com> | 2014-12-16 11:31:32 -0600 |
commit | a3609d2014a5d4f79b621e40495ac02d859a47d8 (patch) | |
tree | fcec9ceef85c8fb6ff26872c0070d11b8d22635a /src | |
parent | 175fded9739b7558ecab008a8e8459db2993e565 (diff) | |
download | gatekeeper-a3609d2014a5d4f79b621e40495ac02d859a47d8.zip gatekeeper-a3609d2014a5d4f79b621e40495ac02d859a47d8.tar.gz gatekeeper-a3609d2014a5d4f79b621e40495ac02d859a47d8.tar.bz2 |
updating the Gatekeeper class with delete handling
Diffstat (limited to 'src')
-rw-r--r-- | src/Psecio/Gatekeeper/Gatekeeper.php | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/Psecio/Gatekeeper/Gatekeeper.php b/src/Psecio/Gatekeeper/Gatekeeper.php index 28a57e2..34eef92 100644 --- a/src/Psecio/Gatekeeper/Gatekeeper.php +++ b/src/Psecio/Gatekeeper/Gatekeeper.php @@ -136,6 +136,8 @@ class Gatekeeper return self::handleFindBy($name, $args); } elseif ($action == 'create') { return self::handleCreate($name, $args); + } elseif ($action == 'delete') { + return self::handleDelete($name, $args); } return false; } @@ -194,4 +196,45 @@ class Gatekeeper } 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 $model->delete(); + } + + /** + * Build the model instance with data given + * + * @param string $action Action called (ex: "delete" or "create") + * @param string $name Function nname + * @param array $args Arguments set + * @throws \Exception\ModelNotFoundException If model type is not found + * @return object Model instance + */ + protected static function buildModel($action = 'find', $name, array $args) + { + $name = str_replace($action, '', $name); + preg_match('/By(.+)/', $name, $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::$pdo); + $instance->find($data); + return $instance; + } }
\ No newline at end of file |