Overview

Namespaces

  • NGS
    • Client
      • Exception
    • Converter
    • Patterns
  • PHP

Classes

  • ApplicationProxy
  • CrudProxy
  • DomainProxy
  • HttpRequest
  • QueryString
  • ReportingProxy
  • RestHttp
  • StandardProxy
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: namespace NGS\Client;
  3: 
  4: require_once(__DIR__.'/../Utils.php');
  5: require_once(__DIR__.'/../Name.php');
  6: require_once(__DIR__.'/RestHttp.php');
  7: 
  8: use NGS\Utils;
  9: use NGS\Name;
 10: 
 11: /**
 12:  * Proxy service to remote RPC-like API
 13:  *
 14:  * Remote services can be called using their name
 15:  *
 16:  * @package NGS\Client
 17:  */
 18: class ApplicationProxy
 19: {
 20:     const APPLICATION_URI = 'RestApplication.svc';
 21: 
 22:     protected $http;
 23: 
 24:     protected static $instance;
 25: 
 26:     /**
 27:      * Create a new ApplicationProxy instance
 28:      *
 29:      * @param RestHttp $http RestHttp instance used for http request.
 30:      * Optionally specify an instance, otherwise use a singleton instance
 31:      */
 32:     public function __construct(RestHttp $http = null)
 33:     {
 34:         $this->http = $http !== null ? $http : RestHttp::instance();
 35:     }
 36: 
 37:     /**
 38:      * Gets singleton instance of RestApplication.svc proxy
 39:      *
 40:      * @return ApplicationProxy
 41:      */
 42:     public static function instance()
 43:     {
 44:         if(self::$instance === null)
 45:             self::$instance = new ApplicationProxy();
 46:         return self::$instance;
 47:     }
 48: 
 49:     /**
 50:      * If remote service doesn't require any arguments it can be called using get method.
 51:      *
 52:      * @param        $command
 53:      * @param array  $expectedCode
 54:      * @param string $accept
 55:      * @return mixed
 56:      */
 57:     public function get($command, array $expectedCode = array(200), $accept = 'application/json')
 58:     {
 59:         return
 60:             $this->http->sendRequest(
 61:                 self::APPLICATION_URI.'/'.rawurlencode($command),
 62:                 'GET',
 63:                 null,
 64:                 $expectedCode,
 65:                 $accept);
 66:     }
 67: 
 68:     /**
 69:      * When remote service requires an argument, message with serialized payload will be sent.
 70:      *
 71:      * @param string $command
 72:      * @param array  $data
 73:      * @param array  $expectedCode
 74:      * @param string $accept
 75:      * @return mixed
 76:      */
 77:     public function post(
 78:         $command,
 79:         array $data = null,
 80:         array $expectedCode = array(200),
 81:         $accept = 'application/json')
 82:     {
 83:         return
 84:             $this->postJson(
 85:                 $command,
 86:                 $data !== null ? json_encode($data) : null,
 87:                 $expectedCode,
 88:                 $accept
 89:             );
 90:     }
 91: 
 92:     /**
 93:      * As {@see post}, when arguments are already serialized in JSON
 94:      *
 95:      * @param  string $command
 96:      * @param  string $data         JSON encoded data
 97:      * @param  array  $expectedCode
 98:      * @param  string $accept
 99:      * @return mixed
100:      */
101:     public function postJson(
102:         $command,
103:         $data = null,
104:         array $expectedCode = array(200),
105:         $accept = 'application/json')
106:     {
107:         if(!is_string($data) && $data !== null)
108:             throw new \InvalidArgumentException('Data must be encoded in json string or null. Data was "'.\NGS\Utils\gettype($data).'"');
109:         return
110:             $this->http->sendRequest(
111:                 self::APPLICATION_URI.'/'.rawurlencode($command),
112:                 'POST',
113:                 $data,
114:                 $expectedCode,
115:                 $accept);
116:     }
117: }
118: 
API documentation generated by ApiGen 2.8.0