MonsterInsights_GA_Lib_Service_Resource

Implements the actual methods/resources of the discovered Google API using magic function calling overloading (__call()), which on call will see if the method name (plus.activities.list) is available in this service, and if so construct an apiHttpRequest representing it.

Defined (1)

The class is defined in the following location(s).

/assets/lib/google/src/Google/Service/Resource.php  
  1. class MonsterInsights_GA_Lib_Service_Resource 
  2. // Valid query parameters that work, but don't appear in discovery. 
  3. private $stackParameters = array( 
  4. 'alt' => array('type' => 'string', 'location' => 'query'),  
  5. 'fields' => array('type' => 'string', 'location' => 'query'),  
  6. 'trace' => array('type' => 'string', 'location' => 'query'),  
  7. 'userIp' => array('type' => 'string', 'location' => 'query'),  
  8. 'quotaUser' => array('type' => 'string', 'location' => 'query'),  
  9. 'data' => array('type' => 'string', 'location' => 'body'),  
  10. 'mimeType' => array('type' => 'string', 'location' => 'header'),  
  11. 'uploadType' => array('type' => 'string', 'location' => 'query'),  
  12. 'mediaUpload' => array('type' => 'complex', 'location' => 'query'),  
  13. 'prettyPrint' => array('type' => 'string', 'location' => 'query'),  
  14. ); 
  15.  
  16. /** @var string $rootUrl */ 
  17. private $rootUrl; 
  18.  
  19. /** @var MonsterInsights_GA_Lib_Client $client */ 
  20. private $client; 
  21.  
  22. /** @var string $serviceName */ 
  23. private $serviceName; 
  24.  
  25. /** @var string $servicePath */ 
  26. private $servicePath; 
  27.  
  28. /** @var string $resourceName */ 
  29. private $resourceName; 
  30.  
  31. /** @var array $methods */ 
  32. private $methods; 
  33.  
  34. public function __construct($service, $serviceName, $resourceName, $resource) 
  35. $this->rootUrl = $service->rootUrl; 
  36. $this->client = $service->getClient(); 
  37. $this->servicePath = $service->servicePath; 
  38. $this->serviceName = $serviceName; 
  39. $this->resourceName = $resourceName; 
  40. $this->methods = is_array($resource) && isset($resource['methods']) ? 
  41. $resource['methods'] : 
  42. array($resourceName => $resource); 
  43.  
  44. /** 
  45. * TODO: This function needs simplifying. 
  46. * @param $name 
  47. * @param $arguments 
  48. * @param $expected_class - optional, the expected class name 
  49. * @return MonsterInsights_GA_Lib_Http_Request|expected_class 
  50. * @throws MonsterInsights_GA_Lib_Exception 
  51. */ 
  52. public function call($name, $arguments, $expected_class = null) 
  53. if (! isset($this->methods[$name])) { 
  54. $this->client->getLogger()->error( 
  55. 'Service method unknown',  
  56. array( 
  57. 'service' => $this->serviceName,  
  58. 'resource' => $this->resourceName,  
  59. 'method' => $name 
  60. ); 
  61.  
  62. throw new MonsterInsights_GA_Lib_Exception( 
  63. "Unknown function: " . 
  64. "{$this->serviceName}->{$this->resourceName}->{$name}()" 
  65. ); 
  66. $method = $this->methods[$name]; 
  67. $parameters = $arguments[0]; 
  68.  
  69. // postBody is a special case since it's not defined in the discovery 
  70. // document as parameter, but we abuse the param entry for storing it. 
  71. $postBody = null; 
  72. if (isset($parameters['postBody'])) { 
  73. if ($parameters['postBody'] instanceof MonsterInsights_GA_Lib_Model) { 
  74. // In the cases the post body is an existing object, we want 
  75. // to use the smart method to create a simple object for 
  76. // for JSONification. 
  77. $parameters['postBody'] = $parameters['postBody']->toSimpleObject(); 
  78. } else if (is_object($parameters['postBody'])) { 
  79. // If the post body is another kind of object, we will try and 
  80. // wrangle it into a sensible format. 
  81. $parameters['postBody'] = 
  82. $this->convertToArrayAndStripNulls($parameters['postBody']); 
  83. $postBody = json_encode($parameters['postBody']); 
  84. if ($postBody === false && $parameters['postBody'] !== false) { 
  85. throw new MonsterInsights_GA_Lib_Exception("JSON encoding failed. Ensure all strings in the request are UTF-8 encoded."); 
  86. unset($parameters['postBody']); 
  87.  
  88. // TODO: optParams here probably should have been 
  89. // handled already - this may well be redundant code. 
  90. if (isset($parameters['optParams'])) { 
  91. $optParams = $parameters['optParams']; 
  92. unset($parameters['optParams']); 
  93. $parameters = array_merge($parameters, $optParams); 
  94.  
  95. if (!isset($method['parameters'])) { 
  96. $method['parameters'] = array(); 
  97.  
  98. $method['parameters'] = array_merge( 
  99. $this->stackParameters,  
  100. $method['parameters'] 
  101. ); 
  102. foreach ($parameters as $key => $val) { 
  103. if ($key != 'postBody' && ! isset($method['parameters'][$key])) { 
  104. $this->client->getLogger()->error( 
  105. 'Service parameter unknown',  
  106. array( 
  107. 'service' => $this->serviceName,  
  108. 'resource' => $this->resourceName,  
  109. 'method' => $name,  
  110. 'parameter' => $key 
  111. ); 
  112. throw new MonsterInsights_GA_Lib_Exception("($name) unknown parameter: '$key'"); 
  113.  
  114. foreach ($method['parameters'] as $paramName => $paramSpec) { 
  115. if (isset($paramSpec['required']) && 
  116. $paramSpec['required'] && 
  117. ! isset($parameters[$paramName]) 
  118. ) { 
  119. $this->client->getLogger()->error( 
  120. 'Service parameter missing',  
  121. array( 
  122. 'service' => $this->serviceName,  
  123. 'resource' => $this->resourceName,  
  124. 'method' => $name,  
  125. 'parameter' => $paramName 
  126. ); 
  127. throw new MonsterInsights_GA_Lib_Exception("($name) missing required param: '$paramName'"); 
  128. if (isset($parameters[$paramName])) { 
  129. $value = $parameters[$paramName]; 
  130. $parameters[$paramName] = $paramSpec; 
  131. $parameters[$paramName]['value'] = $value; 
  132. unset($parameters[$paramName]['required']); 
  133. } else { 
  134. // Ensure we don't pass nulls. 
  135. unset($parameters[$paramName]); 
  136.  
  137. $this->client->getLogger()->info( 
  138. 'Service Call',  
  139. array( 
  140. 'service' => $this->serviceName,  
  141. 'resource' => $this->resourceName,  
  142. 'method' => $name,  
  143. 'arguments' => $parameters,  
  144. ); 
  145.  
  146. $url = MonsterInsights_GA_Lib_Http_REST::createRequestUri( 
  147. $this->servicePath,  
  148. $method['path'],  
  149. $parameters 
  150. ); 
  151. $httpRequest = new MonsterInsights_GA_Lib_Http_Request( 
  152. $url,  
  153. $method['httpMethod'],  
  154. null,  
  155. $postBody 
  156. ); 
  157.  
  158. if ($this->rootUrl) { 
  159. $httpRequest->setBaseComponent($this->rootUrl); 
  160. } else { 
  161. $httpRequest->setBaseComponent($this->client->getBasePath()); 
  162.  
  163. if ($postBody) { 
  164. $contentTypeHeader = array(); 
  165. $contentTypeHeader['content-type'] = 'application/json; charset=UTF-8'; 
  166. $httpRequest->setRequestHeaders($contentTypeHeader); 
  167. $httpRequest->setPostBody($postBody); 
  168.  
  169. $httpRequest = $this->client->getAuth()->sign($httpRequest); 
  170. $httpRequest->setExpectedClass($expected_class); 
  171.  
  172. if (isset($parameters['data']) && 
  173. ($parameters['uploadType']['value'] == 'media' || $parameters['uploadType']['value'] == 'multipart')) { 
  174. // If we are doing a simple media upload, trigger that as a convenience. 
  175. $mfu = new MonsterInsights_GA_Lib_Http_MediaFileUpload( 
  176. $this->client,  
  177. $httpRequest,  
  178. isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream',  
  179. $parameters['data']['value'] 
  180. ); 
  181.  
  182. if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') { 
  183. $httpRequest->enableExpectedRaw(); 
  184.  
  185. if ($this->client->shouldDefer()) { 
  186. // If we are in batch or upload mode, return the raw request. 
  187. return $httpRequest; 
  188.  
  189. return $this->client->execute($httpRequest); 
  190.  
  191. protected function convertToArrayAndStripNulls($o) 
  192. $o = (array) $o; 
  193. foreach ($o as $k => $v) { 
  194. if ($v === null) { 
  195. unset($o[$k]); 
  196. } elseif (is_object($v) || is_array($v)) { 
  197. $o[$k] = $this->convertToArrayAndStripNulls($o[$k]); 
  198. return $o;