Refactoring Podcast classes and services
This commit is contained in:
parent
cfb21f8425
commit
d647bf67ea
4 changed files with 133 additions and 26 deletions
|
@ -15,22 +15,40 @@
|
|||
*/
|
||||
class Podcast extends BasePodcast
|
||||
{
|
||||
/*public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false)
|
||||
/**
|
||||
* Override this function so it returns its child class fields as well.
|
||||
* Child class will either be ImportedPodcast or StationPodcast
|
||||
*
|
||||
* @param string $keyType
|
||||
* @param bool $includeLazyLoadColumns
|
||||
* @param array $alreadyDumpedObjects
|
||||
* @param bool $includeForeignObjects
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false)
|
||||
{
|
||||
|
||||
$podcastArray = parent::toArray(BasePeer::TYPE_FIELDNAME);
|
||||
$podcastArray["url"] = $this->getDbUrl();
|
||||
|
||||
//$array = array_merge($podcastArray, $importedPodcastArray);
|
||||
$importedPodcast = ImportedPodcastQuery::create()->filterByDbPodcastId($this->getDbId())->findOne();
|
||||
$stationPodcast = StationPodcastQuery::create()->filterByDbPodcastId($this->getDbId())->findOne();
|
||||
if (!is_null($importedPodcast)) {
|
||||
$importedPodcastArray = $importedPodcast->toArray(BasePeer::TYPE_FIELDNAME);
|
||||
|
||||
//unset podcast_id because we already have that value in $importedPodcastArray
|
||||
//unset($array["podcast_id"]);
|
||||
//unset these values because we already have the podcast id in $podcastArray
|
||||
//and we don't need the imported podcast ID
|
||||
unset($importedPodcastArray["id"]);
|
||||
unset($importedPodcastArray["podcast_id"]);
|
||||
|
||||
return array_merge($podcastArray, $importedPodcastArray);
|
||||
|
||||
} else if (!is_null($stationPodcast)) {
|
||||
// For now just return $podcastArray because StationPodcast objects do not have any
|
||||
// extra fields we want to return right now. This may change in the future.
|
||||
return $podcastArray;
|
||||
} else {
|
||||
return $podcastArray;
|
||||
}
|
||||
|
||||
return $podcastArray;
|
||||
}
|
||||
|
||||
public function getDbUrl()
|
||||
{
|
||||
return $this->getImportedPodcasts()->getFirst()->getDbUrl();
|
||||
}*/
|
||||
}
|
||||
|
|
|
@ -62,7 +62,7 @@ class Rest_PodcastController extends Zend_Rest_Controller
|
|||
try {
|
||||
$this->getResponse()
|
||||
->setHttpResponseCode(200)
|
||||
->appendBody(json_encode(Podcast::getPodcastById($id)));
|
||||
->appendBody(json_encode(Application_Service_PodcastService::getPodcastById($id)));
|
||||
} catch (PodcastNotFoundException $e) {
|
||||
$this->podcastNotFoundResponse();
|
||||
Logging::error($e->getMessage());
|
||||
|
@ -127,8 +127,8 @@ class Rest_PodcastController extends Zend_Rest_Controller
|
|||
// to prevent the user from trying to download them again while Celery is running
|
||||
$episodes = $this->_service->addPodcastEpisodePlaceholders($requestData["podcast"]["id"],
|
||||
$requestData["podcast"]["episodes"]);
|
||||
$this->_service->downloadEpisodes($episodes);
|
||||
$podcast = Podcast::updateFromArray($id, $requestData);
|
||||
//$this->_service->downloadEpisodes($episodes);
|
||||
$podcast = Application_Service_PodcastService::updatePodcastFromArray($id, $requestData);
|
||||
|
||||
$this->getResponse()
|
||||
->setHttpResponseCode(201)
|
||||
|
@ -152,7 +152,7 @@ class Rest_PodcastController extends Zend_Rest_Controller
|
|||
}
|
||||
|
||||
try {
|
||||
Podcast::deleteById($id);
|
||||
Application_Service_PodcastService::deletePodcastById($id);
|
||||
$this->getResponse()
|
||||
->setHttpResponseCode(204);
|
||||
}
|
||||
|
@ -185,13 +185,13 @@ class Rest_PodcastController extends Zend_Rest_Controller
|
|||
switch($method) {
|
||||
case "DELETE":
|
||||
foreach($ids as $id) {
|
||||
Podcast::deleteById($id);
|
||||
Application_Service_PodcastService::deletePodcastById($id);
|
||||
$responseBody = "Success"; // TODO: make this more descriptive
|
||||
}
|
||||
break;
|
||||
case "GET":
|
||||
foreach($ids as $id) {
|
||||
$podcast = Podcast::getPodcastById($id);
|
||||
$podcast = Application_Service_PodcastService::getPodcastById($id);
|
||||
$responseBody[] = array(
|
||||
"podcast"=>json_encode($podcast),
|
||||
"html"=>$this->view->render($path),
|
||||
|
|
|
@ -6,6 +6,6 @@ class PodcastFactory
|
|||
{
|
||||
// check if station podcast exists and if not, create one
|
||||
|
||||
return Application_Service_ImportedPodcastService::createFromFeedUrl($feedUrl);
|
||||
return Application_Service_PodcastService::createFromFeedUrl($feedUrl);
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ class InvalidPodcastException extends Exception {}
|
|||
class PodcastNotFoundException extends Exception {}
|
||||
|
||||
|
||||
class Application_Service_ImportedPodcastService
|
||||
class Application_Service_PodcastService
|
||||
{
|
||||
|
||||
// These fields should never be modified with POST/PUT data
|
||||
|
@ -25,9 +25,9 @@ class Application_Service_ImportedPodcastService
|
|||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function importedPodcastLimitReached()
|
||||
public static function PodcastLimitReached()
|
||||
{
|
||||
if (ImportedPodcastQuery::create()->count() >= 50) {
|
||||
if (PodcastQuery::create()->count() >= 50) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
@ -66,7 +66,7 @@ class Application_Service_ImportedPodcastService
|
|||
*/
|
||||
public static function createFromFeedUrl($feedUrl)
|
||||
{
|
||||
if (self::importedPodcastLimitReached()) {
|
||||
if (self::PodcastLimitReached()) {
|
||||
throw new PodcastLimitReachedException();
|
||||
}
|
||||
|
||||
|
@ -130,7 +130,7 @@ class Application_Service_ImportedPodcastService
|
|||
$importedPodcast->setPodcast($podcast);
|
||||
$importedPodcast->save();
|
||||
|
||||
return self::_generatePodcastArray($importedPodcast, $rss);
|
||||
return self::_generatePodcastArray($podcast, $rss);
|
||||
} catch(Exception $e) {
|
||||
$podcast->delete();
|
||||
throw $e;
|
||||
|
@ -188,13 +188,13 @@ class Application_Service_ImportedPodcastService
|
|||
* Given a podcast object and a SimplePie feed object,
|
||||
* generate a data array to pass back to the front-end
|
||||
*
|
||||
* @param $importedPodcast ImportedPodcast model object
|
||||
* @param $podcast Podcast model object
|
||||
* @param SimplePie $rss SimplePie feed object
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function _generatePodcastArray($importedPodcast, $rss) {
|
||||
$podcastArray = $importedPodcast->toArray(BasePeer::TYPE_FIELDNAME);
|
||||
private static function _generatePodcastArray($podcast, $rss) {
|
||||
$podcastArray = $podcast->toArray(BasePeer::TYPE_FIELDNAME);
|
||||
|
||||
$podcastArray["episodes"] = array();
|
||||
foreach ($rss->get_items() as $item) {
|
||||
|
@ -212,4 +212,93 @@ class Application_Service_ImportedPodcastService
|
|||
|
||||
return $podcastArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a Podcast's rss feed and returns all its episodes with
|
||||
* the Podcast object
|
||||
*
|
||||
* @param $podcastId
|
||||
*
|
||||
* @throws PodcastNotFoundException
|
||||
* @throws InvalidPodcastException
|
||||
* @return array - Podcast Array with a full list of episodes
|
||||
*/
|
||||
public static function getPodcastById($podcastId)
|
||||
{
|
||||
$podcast = PodcastQuery::create()->findPk($podcastId);
|
||||
if (!$podcast) {
|
||||
throw new PodcastNotFoundException();
|
||||
}
|
||||
|
||||
// Is it an imported podcast?
|
||||
$importedPodcast = ImportedPodcastQuery::create()->filterByDbPodcastId($podcast->getDbId())->findOne();
|
||||
if (!is_null($importedPodcast)) {
|
||||
|
||||
$rss = self::getPodcastFeed($importedPodcast->getDbUrl());
|
||||
if (!$rss) {
|
||||
throw new InvalidPodcastException();
|
||||
}
|
||||
} else {
|
||||
//TODO: get station podcast
|
||||
}
|
||||
|
||||
return self::_generatePodcastArray($podcast, $rss);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a Podcast and its podcast episodes
|
||||
*
|
||||
* @param $podcastId
|
||||
* @throws Exception
|
||||
* @throws PodcastNotFoundException
|
||||
*/
|
||||
public static function deletePodcastById($podcastId)
|
||||
{
|
||||
$podcast = PodcastQuery::create()->findPk($podcastId);
|
||||
if ($podcast) {
|
||||
$podcast->delete();
|
||||
} else {
|
||||
throw new PodcastNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a Podcast object with the given metadata
|
||||
*
|
||||
* @param $podcastId
|
||||
* @param $data
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @throws PodcastNotFoundException
|
||||
*/
|
||||
public static function updatePodcastFromArray($podcastId, $data)
|
||||
{
|
||||
$podcast = PodcastQuery::create()->findPk($podcastId);
|
||||
if (!$podcast) {
|
||||
throw new PodcastNotFoundException();
|
||||
}
|
||||
|
||||
self::removePrivateFields($data);
|
||||
self::validatePodcastMetadata($data);
|
||||
|
||||
$importedPodcast = ImportedPodcastQuery::create()->filterByDbPodcastId($podcast->getDbId())->findOne();
|
||||
if (!is_null($importedPodcast)) {
|
||||
$importedPodcast->fromArray($data, BasePeer::TYPE_FIELDNAME);
|
||||
$importedPodcast->save();
|
||||
} else {
|
||||
//TODO: station podcast
|
||||
}
|
||||
|
||||
//$podcast->fromArray($data, BasePeer::TYPE_FIELDNAME);
|
||||
//$podcast->save();
|
||||
|
||||
return $podcast->toArray(BasePeer::TYPE_FIELDNAME);
|
||||
}
|
||||
|
||||
private static function removePrivateFields(&$data)
|
||||
{
|
||||
foreach (self::$privateFields as $key) {
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue