MyAllocator BuildToUs API PHP SDK for OTA's
  • Namespace
  • Class
  • Tree

Namespaces

  • MyAllocator
    • phpsdkota
      • src
        • Api
          • Inbound
        • Object

Classes

  • MaRouter

Interfaces

  • MaInboundInterface
  1 <?php
  2 /**
  3  * MyAllocator BuildToUs PHP SDK Inbound API Router
  4  *
  5  *  1. Receives incoming requests from the endpoint receiver.
  6  *  2. Authenticates the request.
  7  *  3. Validates the request data.
  8  *  4. Invokes the backend method.
  9  *  5. Responds to invoking script.
 10  *
 11  * @package     myallocator/myallocator-php-sdk-ota
 12  * @author      Nathan Helenihi <support@myallocator.com>
 13  * @copyright   Copyright (c) MyAllocator
 14  * @license     http://mit-license.org/
 15  * @link        https://github.com/MyAllocator/myallocator-php-sdk-ota
 16  */
 17 
 18 namespace MyAllocator\phpsdkota\src\Api\Inbound;
 19 use MyAllocator\phpsdkota\src\Api\Inbound\MaInboundInterface;
 20 use MyAllocator\phpsdkota\src\Object\MaResponse;
 21 
 22 class MaRouter
 23 {
 24     /**
 25      * @var array Valid API endpoints and their required/optional arguments.
 26      */
 27     protected $apis = null;
 28 
 29     /**
 30      * @var array Interface to OTA backend.
 31      */
 32     protected $ota_intf = null;
 33 
 34     /**
 35      * Class contructor.
 36      *
 37      * @param \MyAllocator\phpsdkota\src\Api\Inbound\MaInboundInterface
 38      *  $interface The object implementing MaInboundInterface to invoke
 39      *  backend functionality.
 40      */
 41     public function __construct(MaInboundInterface $interface)
 42     {
 43         // Load configuration
 44         $this->config = require(dirname(__FILE__) . '/../../Config/MaConfig.php');
 45 
 46         // Load API's
 47         $this->apis = require(dirname(__FILE__) . '/../../Config/MaApisInbound.php');
 48 
 49         // Set inbound interface
 50         $this->ota_intf = $interface;
 51     }
 52 
 53     /**
 54      * Router for inbound requests from MyAllocator.
 55      *
 56      * Receives API request, performs common functionality, and invokes method.
 57      *
 58      * @param string @post_body The JSON encoded post body.
 59      *
 60      * @return array The JSON decoded response array.
 61      */
 62     public function processRequest($post_body = null)
 63     {
 64         $rsp = new \MyAllocator\phpsdkota\src\Object\MaResponse($this->ota_intf);
 65 
 66         // Validate arg
 67         if (!$post_body) {
 68             return $rsp->error(MA_OTA_ERR_ARGS_INVALID)->toArray();
 69         }
 70 
 71         // Get post body
 72         try {
 73             $this->ota_intf->log('Request',$post_body);
 74             $request = $this->decode($post_body);
 75         } catch (\Exception $e) {
 76             return $rsp->error(MA_OTA_ERR_JSON_INVALID, $e->getMessage())->toArray();
 77         }
 78 
 79         // JSON structure validation
 80         if (!is_array($request) || empty($request)) {
 81             return $rsp->error(MA_OTA_ERR_JSON_INVALID)->toArray();
 82         }
 83 
 84         // MA:OTA authentication
 85         if (!isset($request['shared_secret'])) {
 86             return $rsp->error(MA_OTA_ERR_AUTH_INVALID)->toArray();
 87         } else {
 88             if ($request['shared_secret'] !== $this->config['shared_secret']) {
 89                 return $rsp->error(MA_OTA_ERR_AUTH_INVALID)->toArray();
 90             } else {
 91                 // Unset for security
 92                 unset($request['shared_secret']);
 93             }
 94         }
 95 
 96         // API and data validation
 97         if (!isset($request['verb'])) {
 98             return $rsp->error(MA_OTA_ERR_VERB_INVALID)->toArray();
 99         } else if (!isset($this->apis[$request['verb']])) {
100             return $rsp->error(MA_OTA_ERR_VERB_INVALID, $request['verb'])->toArray();
101         } else {
102             // Validate required keys
103             foreach ($this->apis[$request['verb']]['args']['req'] as $key) {
104                 if (!isset($request[$key])) {
105                     return $rsp->error(MA_OTA_ERR_ARGS_INVALID, $key)->toArray();
106                 }
107             }
108             // Prune unexpected keys (not required or optional)
109             foreach ($request as $key => $val) {
110                 if (!in_array($key, $this->apis[$request['verb']]['args']['req']) &&
111                     !in_array($key, $this->apis[$request['verb']]['args']['opt'])
112                 ) {
113                     unset($request[$key]);
114                 }
115             }
116         }
117 
118         // MA:OTA property validation
119         $rsp = $this->ota_intf->authenticateProperty($request);
120         if (!$rsp->success) {
121             return $rsp->response();
122         }
123 
124         // Invoke method
125         switch ($request['verb']) {
126             case 'HealthCheck':
127                 $rsp->success();
128                 break;
129             case 'SetupProperty':
130                 $rsp = $this->ota_intf->setupProperty($request);
131                 break;
132             case 'GetRoomTypes':
133                 $rsp = $this->ota_intf->getRoomTypes($request);
134                 break;
135             case 'GetBookingId':
136                 $rsp = $this->ota_intf->getBookingId($request);
137                 break;
138             case 'GetBookingList':
139                 $rsp = $this->ota_intf->getBookingList($request);
140                 break;
141             case 'ARIUpdate':
142                 $rsp = $this->ota_intf->ARIUpdate($request);
143                 break;
144             default: 
145                 $rsp->error(MA_OTA_ERR_VERB_INVALID);
146                 break;
147         }
148 
149         // Validate response is of type MaResponse
150         if (get_class($rsp) != 'MyAllocator\phpsdkota\src\Object\MaResponse') {
151             $rsp = new \MyAllocator\phpsdkota\src\Object\MaResponse();
152             $rsp->error(MA_OTA_ERR_RSP_INVALID);
153         } else {
154             $rsp->setLogger($this->ota_intf);
155         }
156 
157         return $rsp->response();
158     }
159 
160     private function decode($json)
161     {
162         $result = json_decode($json, true);
163         switch(json_last_error())
164         {
165             case JSON_ERROR_DEPTH:
166                 $error = 'Maximum stack depth exceeded';
167                 break;
168             case JSON_ERROR_CTRL_CHAR:
169                 $error = 'Unexpected control character found';
170                 break;
171             case JSON_ERROR_SYNTAX:
172                 $error = 'Syntax error, malformed JSON';
173                 break;
174             case JSON_ERROR_NONE:
175             default:
176                 $error = '';                    
177         }
178 
179         if (!empty($error)) {
180             throw new \Exception('JSON Error: '.$error);        
181         }
182         
183         return $result;
184     }
185 }
186 
MyAllocator BuildToUs API PHP SDK for OTA's API documentation generated by ApiGen