diff --git a/lib/Tmdb/Client.php b/lib/Tmdb/Client.php index 07ca5100..703612b2 100644 --- a/lib/Tmdb/Client.php +++ b/lib/Tmdb/Client.php @@ -29,7 +29,20 @@ * @package Tmdb */ class Client { - const TMDB_URI = 'http://api.themoviedb.org/3/'; + /** + * Base API URI + */ + const TMDB_URI = '//api.themoviedb.org/3/'; + + /** + * Insecure schema + */ + const SCHEME_INSECURE = 'http'; + + /** + * Secure schema + */ + const SCHEME_SECURE = 'https'; /** * Stores API authentication token @@ -38,6 +51,13 @@ class Client { */ private $token; + /** + * Whether the request is supposed to use a secure schema + * + * @var bool + */ + private $secure = false; + /** * Stores the HTTP Client * @@ -45,17 +65,19 @@ class Client { */ private $httpClient; - private $options = array(); - /** * Construct our client * - * @param ClientInterface $httpClient - * @param Token $token + * @param ClientInterface|null $httpClient + * @param Token|null $token + * @param boolean $secure */ - public function __construct(Token $token, ClientInterface $httpClient = null) + public function __construct(Token $token, ClientInterface $httpClient = null, $secure = false) { - $httpClient = $httpClient ?: new GuzzleClient(self::TMDB_URI); + $this->setToken($token); + $this->setSecure($secure); + + $httpClient = $httpClient ?: new GuzzleClient($this->getBaseUrl()); if ($httpClient instanceof \Guzzle\Common\HasDispatcherInterface) { $apiTokenPlugin = new ApiTokenPlugin($token); @@ -65,8 +87,7 @@ public function __construct(Token $token, ClientInterface $httpClient = null) $httpClient->addSubscriber($acceptJsonHeaderPlugin); } - $this->httpClient = new HttpClient(self::TMDB_URI, array(), $httpClient); - $this->setToken($token); + $this->httpClient = new HttpClient($this->getBaseUrl(), array(), $httpClient); } /** @@ -258,34 +279,35 @@ public function setHeaders(array $headers) } /** - * @param string $name + * Return the base url with preferred schema * - * @return mixed - * - * @throws InvalidArgumentException + * @return string */ - public function getOption($name) + private function getBaseUrl() { - if (!array_key_exists($name, $this->options)) { - throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name)); - } - - return $this->options[$name]; + return sprintf( + '%s:%s', + $this->getSecure() ? self::SCHEME_SECURE : self::SCHEME_INSECURE, + self::TMDB_URI + ); } /** - * @param string $name - * @param mixed $value - * - * @throws InvalidArgumentException - * @throws InvalidArgumentException + * @param boolean $secure + * @return $this */ - public function setOption($name, $value) + public function setSecure($secure) { - if (!array_key_exists($name, $this->options)) { - throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name)); - } + $this->secure = $secure; + return $this; + } - $this->options[$name] = $value; + /** + * @return boolean + */ + public function getSecure() + { + return $this->secure; } + } \ No newline at end of file