-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed the entire model organization
The base model is Transmission\Torrent now which extends from Transmission\Model\Torrent. This way, all the static methods are neatly organized in the base torrent model (Transmission\Torrent) while the actual representation is done by the actual model (Transmission\Model\Torrent). This has the adventage that new fields are more easy to add since it doens't make the entire model less easy to read.
- Loading branch information
Showing
9 changed files
with
729 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
namespace Transmission\Model; | ||
|
||
use Transmission\Client; | ||
|
||
/** | ||
* Represents a torrent in Transmissions download queue | ||
* | ||
* @author Ramon Kleiss <[email protected]> | ||
*/ | ||
class Torrent extends AbstractModel | ||
{ | ||
/** | ||
* @var integer | ||
*/ | ||
protected $id; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param Transmission\Client $client | ||
*/ | ||
public function __construct(Client $client = null) | ||
{ | ||
parent::__construct($client); | ||
} | ||
|
||
/** | ||
* @param integer $id | ||
*/ | ||
public function setId($id) | ||
{ | ||
$this->id = (integer) $id; | ||
} | ||
|
||
/** | ||
* @return integer | ||
*/ | ||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
*/ | ||
public function setName($name) | ||
{ | ||
$this->name = (string) $name; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected static function getMapping() | ||
{ | ||
return array( | ||
'id' => 'id', | ||
'name' => 'name' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
<?php | ||
namespace Transmission; | ||
|
||
use Transmission\Model\Torrent as BaseTorrent; | ||
use Transmission\Exception\NoSuchTorrentException; | ||
use Transmission\Exception\InvalidResponseException; | ||
|
||
class Torrent extends BaseTorrent | ||
{ | ||
/** | ||
* Get all the torrents in the download queue | ||
* | ||
* @param Transmission\Client $client | ||
* @return array | ||
*/ | ||
public static function all(Client $client = null) | ||
{ | ||
$client = $client ?: new Client(); | ||
$arguments = array( | ||
'fields' => array(array_keys(self::getMapping())) | ||
); | ||
|
||
$response = $client->call('torrent-get', $arguments); | ||
|
||
self::validateResponse($response, 'all'); | ||
|
||
$torrents = array(); | ||
|
||
foreach ($response->arguments->torrents as $t) { | ||
$torrents[] = self::transformTorrent($t, $client); | ||
} | ||
|
||
return $torrents; | ||
} | ||
|
||
/** | ||
* Get a torrent from the download queue by id | ||
* | ||
* @param integer $id | ||
* @param Transmission\Client $client | ||
* @return Transmission\Torrent | ||
*/ | ||
public static function get($id, Client $client = null) | ||
{ | ||
$client = $client ?: new Client(); | ||
$arguments = array( | ||
'fields' => array(array_keys(self::getMapping())), | ||
'ids' => array($id) | ||
); | ||
|
||
$response = $client->call('torrent-get', $arguments); | ||
|
||
self::validateResponse($response, 'get'); | ||
|
||
return self::transformTorrent( | ||
$response->arguments->torrents[0], | ||
$client | ||
); | ||
} | ||
|
||
/** | ||
* Add a torrent to the download queue | ||
* | ||
* @param string $torrent | ||
* @param Transmission\Client $client | ||
* @param boolean $meta | ||
* @return Transmission\Torrent | ||
*/ | ||
public static function add($torrent, Client $client = null, $meta = false) | ||
{ | ||
$client = $client ?: new Client(); | ||
$arguments = array(); | ||
$property = 'torrent-added'; | ||
|
||
$arguments[$meta ? 'metainfo' : 'torrent'] = (string) $torrent; | ||
|
||
$response = $client->call('torrent-add', $arguments); | ||
|
||
self::validateResponse($response, 'add'); | ||
|
||
return self::transformTorrent( | ||
$response->arguments->$property, $client | ||
); | ||
} | ||
|
||
/** | ||
* Remove a torrent from the download queue | ||
* | ||
* @param boolean $localData | ||
*/ | ||
public function delete($localData = false) | ||
{ | ||
$arguments = array( | ||
'ids' => array($this->getId()), | ||
'delete-local-data' => $localData | ||
); | ||
|
||
$response = $this->getClient()->call('torrent-remove', $arguments); | ||
|
||
self::validateResponse($response, 'delete'); | ||
} | ||
|
||
/** | ||
* @param stdClass $torrentData | ||
* @param Transmission\Client $client | ||
* @return Transmission\Torrent | ||
*/ | ||
private static function transformTorrent(\stdClass $torrentData, Client $client) | ||
{ | ||
return ResponseTransformer::transform( | ||
$torrentData, | ||
new Torrent($client), | ||
self::getMapping() | ||
); | ||
} | ||
|
||
/** | ||
* @param stdClass $response | ||
* @param string $method | ||
*/ | ||
private static function validateResponse(\stdClass $response, $method = null) | ||
{ | ||
if (!isset($response->result)) { | ||
throw new InvalidResponseException( | ||
'Invalid response received from Transmission' | ||
); | ||
} | ||
|
||
if ($response->result !== 'success') { | ||
throw new \RuntimeException(sprintf( | ||
'An error occured: "%s"', $response->result | ||
)); | ||
} | ||
|
||
switch ($method) { | ||
case 'get': | ||
return self::validateGetResponse($response); | ||
case 'all': | ||
return self::validateAllResponse($response); | ||
case 'add': | ||
return self::validateAddResponse($response); | ||
} | ||
} | ||
|
||
/** | ||
* @param stdClass $response | ||
*/ | ||
private static function validateGetResponse(\stdClass $response) | ||
{ | ||
if (!isset($response->arguments) || | ||
!isset($response->arguments->torrents)) { | ||
throw new InvalidResponseException( | ||
'Invalid response received from Transmission' | ||
); | ||
} | ||
|
||
if (!count($response->arguments->torrents)) { | ||
throw new NoSuchTorrentException('Torrent not found in queue'); | ||
} | ||
} | ||
|
||
/** | ||
* @param stdClass $response | ||
*/ | ||
private static function validateAllResponse(\stdClass $response) | ||
{ | ||
if (!isset($response->arguments) || | ||
!isset($response->arguments->torrents)) { | ||
throw new InvalidResponseException( | ||
'Invalid response received from Transmission' | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param stdClass $response | ||
*/ | ||
private static function validateAddResponse(\stdClass $response) | ||
{ | ||
$torrentField = 'torrent-added'; | ||
|
||
if (!isset($response->arguments) || | ||
!isset($response->arguments->$torrentField) || | ||
empty($response->arguments->$torrentField)) { | ||
throw new InvalidResponseException( | ||
'Invalid response received from Transmission' | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.