Пример #1
0
 /**
  * Call and process the 'virtual' method as defined in Client.php
  *
  * @param string $method
  * @param array $arguments
  * @param array $body
  * @return mixed
  * @throws Exception\AuthorizationRequiredException
  * @throws Exception\RuntimeException
  */
 public function call($method, $arguments = array(), $body = array())
 {
     if ($this->validate_arguments($method, $arguments) === true) {
         $headers = array();
         $headers['Accept'] = 'application/vnd.twitchtv.v3+json';
         $headers['Client-ID'] = $this->client->getClientId();
         if (isset($this->methods[$method]['requiresAuth']) === true) {
             if ($this->methods[$method]['requiresAuth'] === true && !$this->client->getAccessToken()) {
                 throw new Exception\AuthorizationRequiredException('Method: ' . $method . ' requires authorization. Did you forget to use setAccessToken() ?');
             }
         }
         if (isset($this->methods[$method]['requireScope']) == false) {
             /* Should the be an auth exception ? */
             throw new Exception\AuthorizationRequiredException('Method: ' . $method . ' did not set define any scope.');
         } else {
             $client_scope = $this->getClient()->getScope();
             $required_scope = $this->methods[$method]['requireScope'];
             foreach ($required_scope as $scope) {
                 if (in_array($scope, $client_scope) == false) {
                     throw new Exception\AuthorizationRequiredException('Method: ' . $method . ' requires scope: ' . $scope . ' to be set.');
                 }
             }
         }
         if ($this->client->getAccessToken()) {
             $headers['Authorization'] = 'OAuth ' . $this->client->getAccessToken();
         }
         if (isset($this->url_parts[$method]) === true and is_array($this->url_parts[$method]) === true) {
             if (count($this->url_parts[$method]) > 0) {
                 foreach ($this->url_parts[$method] as $key => $url_part) {
                     $this->methods[$method]['path'] = str_replace($key, $url_part['value'], $this->methods[$method]['path']);
                     unset($arguments[$url_part['name']]);
                 }
             }
         }
         $url = '/' . $this->methods[$method]['path'];
         $count = 0;
         while ($value = current($arguments)) {
             $url .= ($count > 0 ? '&' : '?') . key($arguments) . '=' . urlencode($value);
             next($arguments);
             $count++;
         }
         $request = new HttpRequest($url, $this->methods[$method]['httpMethod'], $headers, $body);
         $response = $this->client->getTransport()->sendRequest($request);
         return $response;
     }
 }