Skip to content

Instantly share code, notes, and snippets.

@J7mbo
Created October 4, 2013 12:39
Show Gist options
  • Save J7mbo/6825287 to your computer and use it in GitHub Desktop.
Save J7mbo/6825287 to your computer and use it in GitHub Desktop.
An example of a torrent handler, some factories, torrent class and dispatcher.
<?php
interface TorrentHandler
{
public function retrieveTorrents();
public function getTorrent($id);
public function startTorrent(TorrentInterface $torrent);
public function stopTorrent(TorrentInterface $torrent);
public function pauseTorrent(TorrentInterface $torrent);
public function deleteTorrent(TorrentInterface $torrent);
}
class TransmissionTorrentHandler implements TorrentHandler
{
private $dispatcher;
private $factory;
private $torrents;
public function __construct(TorrentFactory $factory, DispatcherInterface $dispatcher)
{
$this->factory = $factory;
$this->dispatcher = $dispatcher;
}
public function retrieveTorrents()
{
// Gets torrent data
$data = $this->dispatcher->dispatch();
// Create new torrent objects using factory and store in $this->torrents
foreach ($data as $torrentData)
{
$obj = $this->factory->create();
$obj->setId($torrentData['whatever']['id']);
$obj->setName($torrentData['whatever']['name']);
//etc
$this->torrents[] = $obj;
}
}
public function stopTorrent(TorrentInterface $torrent)
{
$this->torrents[$torrent]->stop();
}
}
// ---------------------------------------------
class TorrentFactory
{
public function create()
{
return new Torrent();
}
}
interface TorrentInterface
{
public function start();
public function stop();
public function pause();
public function delete();
}
abstract class TorrentBase implements TorrentInterface
{
private $id;
private $name;
private $status;
private $upload;
private $download;
private $ratio;
public function start();
public function stop();
public function pause();
public function delete();
}
class Torrent extends TorrentBase
{
private $id;
private $name;
private $status;
private $upload;
private $download;
private $ratio;
public function start();
public function stop();
public function pause();
public function delete();
}
// ---------------------------------------------------
// This is to actually get the torrent data
// I will be using CommandDispatcher for command line stuff
// You may be using DBDispatcher to get stuff from the DB
interface DispatcherInterface
{
public function dispatch();
}
abstract class CommandDispatcher implements DispatcherInterface
{
public function dispatch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment