Skip to content

Commit

Permalink
Changed the entire model organization
Browse files Browse the repository at this point in the history
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
kleiram committed Apr 12, 2013
1 parent 2aab1b4 commit f71ba13
Show file tree
Hide file tree
Showing 9 changed files with 729 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Transmission/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Client
/**
* @var string
*/
const RPC_PATH = '/transmission/rpc';
const DEFAULT_PATH = '/transmission/rpc';

/**
* @var string
Expand Down Expand Up @@ -217,7 +217,7 @@ protected function getUrl()
'http://%s:%d%s',
$this->getHost(),
$this->getPort(),
self::RPC_PATH
self::DEFAULT_PATH
);
}

Expand Down
75 changes: 75 additions & 0 deletions lib/Transmission/Model/Torrent.php
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'
);
}
}
190 changes: 190 additions & 0 deletions lib/Transmission/Torrent.php
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'
);
}
}
}
Loading

0 comments on commit f71ba13

Please sign in to comment.