From 94b749bfc863d1c0be2569010e70313eb9f86f9a Mon Sep 17 00:00:00 2001 From: Nick Bartkowiak Date: Sat, 6 Apr 2013 18:26:00 -0500 Subject: [PATCH] Added proper error handling at the library level. This basically identifies when the polymorphic item fetching methods come up empty and throws a 404 resource not found exception. I also fixed a bug where doing single item fetch by its key would never work correctly. --- Exception/Server/Library.php | 50 ++++++++++++++++++++++++++++++ Plex.php | 5 +++ Server/Library.php | 15 ++++++--- Server/Library/SectionAbstract.php | 41 ++++++++++++++++++------ 4 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 Exception/Server/Library.php diff --git a/Exception/Server/Library.php b/Exception/Server/Library.php new file mode 100644 index 0000000..a2f1b38 --- /dev/null +++ b/Exception/Server/Library.php @@ -0,0 +1,50 @@ + Nick Bartkowiak + * @copyright (c) 2013 Nick Bartkowiak + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) + * @version 0.0.2 + * + * 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. + */ + +/** + * Exception to be thrown for any problems at the library level. + * + * @category php-plex + * @package Plex_Exception + * @author Nick Bartkowiak + * @copyright (c) 2013 Nick Bartkowiak + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) + * @version 0.0.2 + */ +class Plex_Exception_Server_Library extends Plex_ExceptionAbstract +{ + /** + * List of valid exception types for the library exception class. + * @var mixed[] + */ + protected $validTypes = array( + // Abstract type for specifiying that a resource of type and name was + // not found. + 'RESOURCE_NOT_FOUND' => array( + 'code' => 404, + 'message' => 'The %s "%s" was not found.' + ) + ); +} diff --git a/Plex.php b/Plex.php index 3a5bbfe..8446e96 100644 --- a/Plex.php +++ b/Plex.php @@ -28,12 +28,16 @@ $phpPlexDir = dirname(__FILE__); +// Exception require_once(sprintf('%s/Exception/ExceptionInterface.php', $phpPlexDir)); require_once(sprintf('%s/Exception/ExceptionAbstract.php', $phpPlexDir)); require_once(sprintf('%s/Exception/Machine.php', $phpPlexDir)); require_once(sprintf('%s/Exception/Server.php', $phpPlexDir)); +require_once(sprintf('%s/Exception/Server/Library.php', $phpPlexDir)); +// Machine require_once(sprintf('%s/Machine/MachineInterface.php', $phpPlexDir)); require_once(sprintf('%s/Machine/MachineAbstract.php', $phpPlexDir)); +// Server require_once(sprintf('%s/Server.php', $phpPlexDir)); require_once(sprintf('%s/Server/Library.php', $phpPlexDir)); require_once(sprintf('%s/Server/Library/SectionAbstract.php', $phpPlexDir)); @@ -53,6 +57,7 @@ require_once(sprintf('%s/Server/Library/Item/Artist.php', $phpPlexDir)); require_once(sprintf('%s/Server/Library/Item/Album.php', $phpPlexDir)); require_once(sprintf('%s/Server/Library/Item/Track.php', $phpPlexDir)); +// Client require_once(sprintf('%s/Client.php', $phpPlexDir)); require_once(sprintf('%s/Client/ControllerAbstract.php', $phpPlexDir)); require_once(sprintf('%s/Client/Controller/Navigation.php', $phpPlexDir)); diff --git a/Server/Library.php b/Server/Library.php index e225c8c..90737c8 100644 --- a/Server/Library.php +++ b/Server/Library.php @@ -7,9 +7,9 @@ * @package Plex_Server * @subpackage Plex_Server_Library * @author Nick Bartkowiak - * @copyright (c) 2012 Nick Bartkowiak + * @copyright (c) 2013 Nick Bartkowiak * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) - * @version 0.0.1 + * @version 0.0.2 * * This file is part of php-plex. * @@ -32,9 +32,9 @@ * @package Plex_Server * @subpackage Plex_Server_Library * @author Nick Bartkowiak - * @copyright (c) 2012 Nick Bartkowiak + * @copyright (c) 2013 Nick Bartkowiak * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) - * @version 0.0.1 + * @version 0.0.2 */ class Plex_Server_Library extends Plex_Server { @@ -258,6 +258,8 @@ public function getSections() * @uses Plex_Server_Library_Section::getKey() * * @return Plex_Server_Library_Section The request library section. + * + * @throws Plex_Exception_Server_Library() */ public function getSectionByKey($key) { @@ -266,6 +268,11 @@ public function getSectionByKey($key) return $section; } } + + throw new Plex_Exception_Server_Library( + 'RESOURCE_NOT_FOUND', + array('section', $key) + ); } /** diff --git a/Server/Library/SectionAbstract.php b/Server/Library/SectionAbstract.php index 977dc53..517eca5 100644 --- a/Server/Library/SectionAbstract.php +++ b/Server/Library/SectionAbstract.php @@ -7,9 +7,9 @@ * @package Plex_Server * @subpackage Plex_Server_Library * @author Nick Bartkowiak - * @copyright (c) 2012 Nick Bartkowiak + * @copyright (c) 2013 Nick Bartkowiak * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) - * @version 0.0.1 + * @version 0.0.2 * * This file is part of php-plex. * @@ -33,9 +33,9 @@ * @package Plex_Server * @subpackage Plex_Server_Library * @author Nick Bartkowiak - * @copyright (c) 2012 Nick Bartkowiak + * @copyright (c) 2013 Nick Bartkowiak * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) - * @version 0.0.1 + * @version 0.0.2 */ abstract class Plex_Server_Library_SectionAbstract extends Plex_Server_Library { @@ -508,6 +508,9 @@ protected function getItemsByYear($year) * @param integer|string $polymorphicData Either a rating key, a key, or a * title for an exact title match that will be used to retrieve a single * library item. + * @param boolean scopedToItem Tells the method whether or not we are + * scoped to an item. If we are scoped to an item then we are use get + * methods instead of search methods. * * @uses Plex_MachineAbstract::getCallingFunction() * @uses Plex_Server_Library::getItems() @@ -519,12 +522,14 @@ protected function getItemsByYear($year) * @uses Plex_Server_Library_SectionAbstract::getPolymorphicItem() * * @return Plex_Server_Library_ItemAbstract The request Plex library item. + * + * @throws Plex_Exception_Server_Library() */ protected function getPolymorphicItem($polymorphicData, $scopedToItem = FALSE) { if (is_int($polymorphicData)) { // If we have an integer then we can assume we have a rating key. - return reset( + if ($item = reset( $this->getItems( sprintf( '%s/%d', @@ -532,9 +537,17 @@ protected function getPolymorphicItem($polymorphicData, $scopedToItem = FALSE) $polymorphicData ) ) + )) { + return $item; + } + + throw new Plex_Exception_Server_Library( + 'RESOURCE_NOT_FOUND', + array('item', $polymorphicData) ); + } else if (strpos($polymorphicData, Plex_Server_Library::ENDPOINT_METADATA) - != FALSE) { + !== FALSE) { // If the single item endpoint appears in the polymorphic data then // is assumed we are dealing with a key, which is already a valid // endpoint. @@ -549,9 +562,16 @@ protected function getPolymorphicItem($polymorphicData, $scopedToItem = FALSE) $polymorphicData ); - return reset($this->getItems($endpoint)); + if ($item = reset($this->getItems($endpoint))) { + return $item; + } + + throw new Plex_Exception_Server_Library( + 'RESOURCE_NOT_FOUND', + array('item', $polymorphicData) + ); + } else { - // If we don't have a rating key or a key then we just assume we're // doing an exact title match. @@ -604,7 +624,10 @@ protected function getPolymorphicItem($polymorphicData, $scopedToItem = FALSE) } // Tried to do an exact title match and came up empty. - return FALSE; + throw new Plex_Exception_Server_Library( + 'RESOURCE_NOT_FOUND', + array('item', $polymorphicData) + ); } }