Skip to content
This repository has been archived by the owner on May 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #9 from MartialGeek/duplicate_torrent
Browse files Browse the repository at this point in the history
Fix #6 : Throw a DuplicateTorrentException when Transmission returns this error
  • Loading branch information
Martial Saunois authored and Martial Saunois committed Mar 8, 2016
2 parents 860496e + 2e11cb5 commit c9cf199
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 4 deletions.
61 changes: 61 additions & 0 deletions src/DuplicateTorrentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,66 @@

class DuplicateTorrentException extends \Exception
{
/**
* @var int
*/
private $torrentId;

/**
* @var string
*/
private $torrentName;

/**
* @var string
*/
private $torrentHashString;

/**
* @return int
*/
public function getTorrentId()
{
return $this->torrentId;
}

/**
* @param int $torrentId
*/
public function setTorrentId($torrentId)
{
$this->torrentId = $torrentId;
}

/**
* @return string
*/
public function getTorrentName()
{
return $this->torrentName;
}

/**
* @param string $torrentName
*/
public function setTorrentName($torrentName)
{
$this->torrentName = $torrentName;
}

/**
* @return string
*/
public function getTorrentHashString()
{
return $this->torrentHashString;
}

/**
* @param string $torrentHashString
*/
public function setTorrentHashString($torrentHashString)
{
$this->torrentHashString = $torrentHashString;
}
}
23 changes: 19 additions & 4 deletions src/RpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,24 @@ public function torrentAdd($sessionId, array $argumentsWithValues)
));
}

$response = $this->sendRequest($sessionId, $this->buildRequestBody(
'torrent-add',
$argumentsWithValues
));
try {
$response = $this->sendRequest($sessionId, $this->buildRequestBody(
'torrent-add',
$argumentsWithValues
));
} catch (TransmissionException $e) {
if ($e->getResult() === 'duplicate torrent') {
$responseArguments = $e->getArguments();
$duplicateTorrentException = new DuplicateTorrentException();
$duplicateTorrentException->setTorrentId($responseArguments['torrent-duplicate']['id']);
$duplicateTorrentException->setTorrentName($responseArguments['torrent-duplicate']['name']);
$duplicateTorrentException->setTorrentHashString($responseArguments['torrent-duplicate']['hashString']);

throw $duplicateTorrentException;
}

throw $e;
}

return $response['arguments']['torrent-added'];
}
Expand Down Expand Up @@ -569,6 +583,7 @@ private function sendRequest($sessionId, $requestBody)
if ($responseBody['result'] !== 'success') {
$e = new TransmissionException('The Transmission RPC API returned an error: ' . $responseBody['result']);
$e->setResult($responseBody['result']);
$e->setArguments($responseBody['arguments']);

if (!is_null($this->logger)) {
$this->logger->error(
Expand Down
21 changes: 21 additions & 0 deletions src/TransmissionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class TransmissionException extends \Exception
*/
private $result;

/**
* @var array
*/
private $arguments;

/**
* @return string
*/
Expand All @@ -24,4 +29,20 @@ public function setResult($result)
{
$this->result = $result;
}

/**
* @return array
*/
public function getArguments()
{
return $this->arguments;
}

/**
* @param array $arguments
*/
public function setArguments(array $arguments)
{
$this->arguments = $arguments;
}
}
35 changes: 35 additions & 0 deletions tests/RpcClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Exception\ClientException;
use Martial\Transmission\API\CSRFException;
use Martial\Transmission\API\DuplicateTorrentException;
use Martial\Transmission\API\RpcClient;
use Martial\Transmission\API\TransmissionException;
use Mockery as m;
Expand Down Expand Up @@ -701,6 +702,40 @@ public function testTorrentAddShouldThrowAnExceptionWithAnInvalidSessionId()
}
}

public function testTorrentAddShouldThrowAnExceptionWithDuplicateTorrent()
{
$success = false;
$arguments = ['filename' => '/path/to/Fedora.torrent'];
$requestBody = '{"method":"torrent-add","arguments":{"filename":"/path/to/Fedora.torrent"}}';
$hashString = md5('Fedora.iso');
$torrentId = 42;
$torrentName = 'Fedora.iso';

$this
->sendRequest($requestBody)
->andReturn($this->guzzleResponse);

$this->setResponseBody(sprintf(
'{"arguments":{"torrent-duplicate":{"id":%d,"name":"%s","hashString":"%s"}},"result":"duplicate torrent"}',
$torrentId,
$torrentName,
$hashString
));

try {
$this->rpcClient->torrentAdd($this->sessionId, $arguments);
} catch (DuplicateTorrentException $e) {
$this->assertSame($torrentId, $e->getTorrentId());
$this->assertSame($torrentName, $e->getTorrentName());
$this->assertSame($hashString, $e->getTorrentHashString());
$success = true;
}

if (!$success) {
$this->fail('DuplicateTorrentException was not thrown.');
}
}

public function testTorrentRemoveWithLocalDataWithSuccess()
{
$requestBody = '{"method":"torrent-remove","arguments":{"ids":[42,1337],"delete-local-data":true}}';
Expand Down

0 comments on commit c9cf199

Please sign in to comment.