miholeus

basic protocol

Jan 16th, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.84 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @namespace
  4.  */
  5. namespace Application\Service\Api\Protocol;
  6. use Application\Service\Api as ApiService;
  7. /**
  8.  * Api Handler BasicProtocol
  9.  *
  10.  * @author miholeus
  11.  */
  12. class Basic extends AbstractProtocol
  13. {
  14.     /**
  15.      *
  16.      * @param string $action service and method name separated by "." symbol
  17.      * @param array $params method params
  18.      * @return mixed
  19.      * @throws \Application\Service\Api\HandlerException
  20.      * @throws \Application\Service\Api\Service\Exception
  21.      */
  22.     public function execute($action, $params)
  23.     {      
  24.         $actionArr = explode(".", $action);
  25.         $serviceName = $actionArr[0];
  26.         // set default service action
  27.         if(!isset($actionArr[1])) {
  28.             $serviceAction = "version";
  29.         } else {
  30.             $serviceAction = $actionArr[1];
  31.         }
  32.         $className = '\\Application\\Service\\Api\\Service\\' . ucfirst($serviceName);
  33.         if(!class_exists($className)) {
  34.             throw new ApiService\HandlerException("Unknown service " . $serviceName);
  35.         }
  36.         /** @var $service \Application\Service\Api\Service\AbstractService */
  37.         $service = new $className($this);
  38.        
  39.         if(!$service instanceof ApiService\ServiceInterface) {
  40.             throw new ApiService\HandlerException("Service " . $serviceName . " should implement"
  41.                     . " \\Application\\Service\\Api\\ServiceInterface");
  42.         }
  43.        
  44.         if(!method_exists($service, $serviceAction)) {
  45.             throw new ApiService\HandlerException(
  46.                     sprintf("Unknown method %s in service %s", $serviceAction, $serviceName)
  47.             );
  48.         }
  49.        
  50.         $this->service = $service;
  51.         $this->service->setParams($params);
  52.  
  53.         return call_user_func_array(array($service, $serviceAction), array($params));
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment