Skip to content

Commit

Permalink
Rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
joedawson committed Sep 18, 2016
1 parent 1ce1e97 commit 150878e
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 43 deletions.
16 changes: 16 additions & 0 deletions src/Helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Dawson\TVDB;

trait Helpers
{
/**
* Client
*
* @return TVDBClient
*/
function client()
{
return new TVDBClient;
}
}
79 changes: 79 additions & 0 deletions src/Resources/Series.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Dawson\TVDB\Resources;

use Dawson\TVDB\TVDB;

class Series extends TVDB
{
/**
* Series ID
*
* @var integer
*/
public $id;

/**
* Constructor
*/
public function __construct(int $id)
{
$this->id = $id;
}

/**
* Get the Series
*
* @return json
*/
public function get()
{
try {
return json_decode($this->client()->login()->client->get('series/' . $this->id)->getBody());
} catch(ClientException $e) {
throw new TVDBException($e);
}
}

/**
* Series Actors
*
* @return json
*/
public function actors()
{
try {
return json_decode($this->client()->login()->client->get('series/' . $this->id . '/actors')->getBody());
} catch(ClientException $e) {
throw new TVDBException($e);
}
}

/**
* Series Episodes
*
* @return json
*/
public function episodes()
{
try {
return json_decode($this->client()->login()->client->get('series/' . $this->id . '/episodes')->getBody());
} catch(ClientException $e) {
throw new TVDBException($e);
}
}

/**
* Series Images
*
* @return json
*/
public function images()
{
try {
return json_decode($this->client()->login()->client->get('series/' . $this->id . '/images')->getBody());
} catch(ClientException $e) {
throw new TVDBException($e);
}
}
}
36 changes: 36 additions & 0 deletions src/TVDB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Dawson\TVDB;

use Dawson\TVDB\Resources;

class TVDB
{
use Helpers;

/**
* Series Resource
*
* @param integer $id
* @return \Dawson\TVDB\Resoures\Series
*/
public function series($id)
{
return new Resources\Series($id);
}

/**
* Search TBDB for Series
*
* @param string $name
* @return json
*/
public function search($name)
{
try {
return json_decode($this->client()->login()->client->get('search/series?name=' . $name)->getBody());
} catch(ClientException $e) {
throw new TVDBException($e);
}
}
}
49 changes: 6 additions & 43 deletions src/TBDB.php → src/TVDBClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@
use GuzzleHttp\Exception\ClientException;
use Psr\Http\Message\RequestInterface;

class TVDB
class TVDBClient
{
/**
* Token for API Requests
*
* @var string
*/
protected $token;

/**
* Constructor
*/
public function __construct()
{
$this->token = null;

$this->username = config('tvdb.username');
$this->userkey = config('tvdb.userkey');
$this->apikey = config('tvdb.apikey');
Expand All @@ -40,46 +35,12 @@ public function __construct()
]);
}

/**
* Search TBDB for Series
*
* @param string $name
* @return json
*/
public function search($name)
{
$this->login();

try {
return json_decode($this->client->get('search/series?name=' . $name)->getBody());
} catch(ClientException $e) {
throw new TVDBException($e);
}
}

/**
* Lookup a Series
*
* @param integer $id
* @return json
*/
public function lookup($id)
{
$this->login();

try {
return json_decode($this->client->get('series/' . $id)->getBody());
} catch(ClientException $e) {
throw new TVDBException($e);
}
}

/**
* Obtain a Token
*
* @return response
*/
private function login()
public function login()
{
try {
$response = $this->client->post('login', ['json' => [
Expand All @@ -89,6 +50,8 @@ private function login()
]])->getBody();

$this->token = json_decode($response->getContents())->token;

return $this;
} catch(ClientException $e) {
throw new TVDBException($e);
}
Expand Down
File renamed without changes.

0 comments on commit 150878e

Please sign in to comment.