Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Commit

Permalink
Added in the abstract controller to Plex client.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbart committed Dec 28, 2012
1 parent b2500c5 commit 978a82f
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 0 deletions.
101 changes: 101 additions & 0 deletions Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class Plex_Client extends Plex_MachineAbstract
*/
private $version;

/**
* The server that registered the client.
* @var Plex_server
*/
private $server;

/**
* The default port on which a Plex client listens.
*/
Expand Down Expand Up @@ -81,6 +87,75 @@ public function __construct($name, $address, $port)
$this->port = $port ? $port : self::DEFAULT_PORT;
}

/**
* Given a controller type, returns an instantiated controller object.
*
* @param string $type The type of controller to be insantiated.
*
* @uses Plex_Client_ControllerAbstract::factory()
* @uses Plex_Client::$name
* @uses Plex_Client::$address
* @uses Plex_Client::$port
* @uses Plex_Client::getServer()
*
* @return Plex_Client_ControllerAbstract The requsted controller.
*/
private function getController($type)
{
return Plex_Client_ControllerAbstract::factory(
$type,
$this->name,
$this->address,
$this->port,
$this->getServer()
);
}

/**
* Returns the navigation controller.
*
* @uses Plex_Client::getController()
* @uses Plex_Client_ControllerAbstract::TYPE_NAVIGATION
*
* @return Plex_Client_Controller_Navigation The navigation controller.
*/
public function getNavigationController()
{
return $this->getController(
Plex_Client_ControllerAbstract::TYPE_NAVIGATION
);
}

/**
* Returns the playback controller.
*
* @uses Plex_Client::getController()
* @uses Plex_Client_ControllerAbstract::TYPE_PLAYBACK
*
* @return Plex_Client_Controller_Playback The playback controller.
*/
public function getPlaybackController()
{
return $this->getController(
Plex_Client_ControllerAbstract::TYPE_PLAYBACK
);
}

/**
* Returns the application controller.
*
* @uses Plex_Client::getController()
* @uses Plex_Client_ControllerAbstract::TYPE_APPLICATION
*
* @return Plex_Client_Controller_Application The application controller.
*/
public function getApplicationController()
{
return $this->getController(
Plex_Client_ControllerAbstract::TYPE_APPLICATION
);
}

/**
* Returns the Plex client's name.
*
Expand Down Expand Up @@ -196,4 +271,30 @@ public function setVersion($version)
{
$this->version = $version;
}

/**
* Returns the server that registered the client.
*
* @uses Plex_Client::$server
*
* @return Plex_Server The server that registered the client.
*/
protected function getServer()
{
return $this->server;
}

/**
* Sets the server that registered the client.
*
* @param Plex_Server $server The server that registered the client.
*
* @uses Plex_Client::$server
*
* @return void
*/
public function setServer(Plex_Server $server)
{
$this->server = $server;
}
}
129 changes: 129 additions & 0 deletions Client/ControllerAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

/**
* Plex Client Controller Abstract
*
* @category php-plex
* @package Plex_Client
* @subpackage Plex_Client_Controller
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2012 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.1
*
* This file is part of php-plex.
*
* php-plex is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* php-plex is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

/**
* Represents a Plex client on the network.
*
* @category php-plex
* @package Plex_Client
* @subpackage Plex_Client_Controller
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2012 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.1
*/
abstract class Plex_Client_ControllerAbstract extends Plex_Client
{
/**
* String representing the navigation controller type.
*/
const TYPE_NAVIGATION = 'navigation';

/**
* String representing the playback controller type.
*/
const TYPE_PLAYBACK = 'playback';

/**
* String representing the applicaton controller type.
*/
const TYPE_APPLICATION = 'application';

/**
* Returns the URL representing a Plex client controller command.
*
* @param string $controller The controller whose command is to be executed.
* @param string $command The command to be executed.
*
* @uses Plex_Client::getServer()
* @uses Plex_Client::getAddress()
* @uses Plex_Server::getBaseUrl()
*
* @return string The URL of the Plex client controller command.
*/
private function buildUrl($controller, $command)
{
return sprintf(
'%s/system/players/%s/%s/%s',
$this->getServer()->getBaseUrl(),
$this->getAddress(),
$controller,
$command
);
}

/**
* Using the calling class and function builds and calls the URL for the
* Plex client controller command.
*
* @uses Plex_MachineAbstract::getCallingFunction()
* @uses Plex_MachineAbstract::makeCall()
* @uses Plex_Client_ControllerAbstract::buildUrl()
*
* @return void
*/
protected function executeCommand()
{
$controller = strtolower(array_pop(explode('_', get_class($this))));
$command = $this->getCallingFunction();
$this->makeCall($this->buildUrl($controller, $command));
}

/**
* Static factory method for instantiating and returning a child controller
* class.
*
* @param string $type The type of the controller to be returned.
* @param string $name The name of the Plex client.
* @param string $address The IP address of the Plex client.
* @param integer $port The port on which the Plex client is listening.
* @param Plex_Server $server The server that registered teh client.
*
* @uses Plex_Client::setServer()
*
* @return Plex_Client_ControllerAbstract The requested controller object.
*/
public static function factory(
$type,
$name,
$address,
$port,
Plex_Server $server
)
{
$classString = sprintf(
'Plex_Client_Controller_%s',
ucfirst($type)
);
$controller = new $classString(
$name,
$address,
$port
);
$controller->setServer($server);
return $controller;
}
}
2 changes: 2 additions & 0 deletions Machine/MachineAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ protected function getBaseUrl()
*/
protected function xmlAttributesToArray($xml)
{
if (!$xml) return false;

$array = array();
$i= 0;
foreach($xml as $node) {
Expand Down
4 changes: 4 additions & 0 deletions Plex.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
require_once(sprintf('%s/Server/Library/Item/Album.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Item/Track.php', $phpPlexDir));
require_once(sprintf('%s/Client.php', $phpPlexDir));
require_once(sprintf('%s/Client/ControllerAbstract.php', $phpPlexDir));
require_once(sprintf('%s/Client/Controller/Navigation.php', $phpPlexDir));
require_once(sprintf('%s/Client/Controller/Playback.php', $phpPlexDir));
require_once(sprintf('%s/Client/Controller/Application.php', $phpPlexDir));

/**
* Bootstrap class for using php-plex to interact with the Plex HTTP API.
Expand Down
2 changes: 2 additions & 0 deletions Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function __construct($name, $address, $port)
* @uses Plex_Client::setHost()
* @uses Plex_Client::setMachineIdentifier()
* @uses Plex_Client::setVersion()
* @uses Plex_Client::setServer()
*
* @return Plex_Client[] An array of Plex clients indexed by the Plex client
* name.
Expand All @@ -102,6 +103,7 @@ public function getClients()
$client->setHost($attribute['host']);
$client->setMachineIdentifier($attribute['machineIdentifier']);
$client->setVersion($attribute['version']);
$client->setServer($this);
$clients[$attribute['name']] = $client;
}

Expand Down

0 comments on commit 978a82f

Please sign in to comment.