2015-10-28 23:54:30 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Application_Service_PublishService {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array map of arbitrary source names to descriptive labels
|
|
|
|
*/
|
|
|
|
private static $SOURCES = array(
|
|
|
|
"soundcloud" => SOUNDCLOUD,
|
2015-11-13 01:02:09 +01:00
|
|
|
"station_podcast" => "My Podcast"
|
2015-10-28 23:54:30 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array map of arbitrary source names to functions that return
|
|
|
|
* their publication state (true = published)
|
|
|
|
*/
|
|
|
|
private static $SOURCE_FUNCTIONS = array(
|
|
|
|
"soundcloud" => "getSoundCloudPublishStatus",
|
|
|
|
"station_podcast" => "getStationPodcastPublishStatus"
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Publish or remove the file with the given file ID from the services
|
|
|
|
* specified in the request data (ie. SoundCloud, the station podcast)
|
|
|
|
*
|
|
|
|
* @param int $fileId ID of the file to be published
|
|
|
|
* @param array $data request data containing what services to publish to
|
|
|
|
*/
|
|
|
|
public static function publish($fileId, $data) {
|
|
|
|
foreach ($data as $k => $v) {
|
|
|
|
$service = PublishServiceFactory::getService($k);
|
2015-10-29 18:09:50 +01:00
|
|
|
$service->$v($fileId);
|
2015-10-28 23:54:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For the file with the given ID, check external sources and generate
|
|
|
|
* an array of source states and labels.
|
|
|
|
*
|
|
|
|
* Sources to which the file has been published should be passed back in a
|
|
|
|
* "published" array, while sources to which the file has not been published
|
|
|
|
* should be passed back in a "toPublish" array.
|
|
|
|
*
|
|
|
|
* @param int $fileId the ID of the file to check
|
|
|
|
*
|
|
|
|
* @return array array containing published and toPublish arrays. Has the form
|
|
|
|
* [
|
|
|
|
* "toPublish" => [
|
|
|
|
* "source" => "label",
|
|
|
|
* ...
|
|
|
|
* ]
|
|
|
|
* "published" => [
|
|
|
|
* "source" => "label",
|
|
|
|
* ...
|
|
|
|
* ]
|
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
public static function getSourceLists($fileId) {
|
2015-11-10 23:54:31 +01:00
|
|
|
$sources = array();
|
2015-10-28 23:54:30 +01:00
|
|
|
foreach (self::$SOURCES as $source => $label) {
|
|
|
|
$fn = self::$SOURCE_FUNCTIONS[$source];
|
|
|
|
// Should be in a ternary but PHP doesn't play nice
|
2015-11-10 23:54:31 +01:00
|
|
|
$status = self::$fn($fileId);
|
|
|
|
array_push($sources, array(
|
|
|
|
"source" => $source,
|
|
|
|
"label" => _($label),
|
|
|
|
"status" => $status
|
|
|
|
));
|
2015-10-28 23:54:30 +01:00
|
|
|
}
|
|
|
|
|
2015-11-10 23:54:31 +01:00
|
|
|
return $sources;
|
2015-10-28 23:54:30 +01:00
|
|
|
}
|
|
|
|
|
2015-10-30 21:10:16 +01:00
|
|
|
/**
|
2015-10-28 23:54:30 +01:00
|
|
|
* Reflective accessor for SoundCloud publication status for the
|
|
|
|
* file with the given ID
|
|
|
|
*
|
|
|
|
* @param int $fileId the ID of the file to check
|
|
|
|
*
|
2015-11-10 23:54:31 +01:00
|
|
|
* @return int 1 if the file has been published to SoundCloud,
|
2015-11-12 20:47:47 +01:00
|
|
|
* 0 if the file has yet to be published,
|
|
|
|
* -1 if the file is in a pending state
|
2015-10-28 23:54:30 +01:00
|
|
|
*/
|
|
|
|
private static function getSoundCloudPublishStatus($fileId) {
|
|
|
|
$soundcloudService = new Application_Service_SoundcloudService();
|
2015-11-12 20:47:47 +01:00
|
|
|
if (!$soundcloudService->hasAccessToken()) { return 2; }
|
2015-10-30 21:10:16 +01:00
|
|
|
return ($soundcloudService->referenceExists($fileId));
|
2015-10-28 23:54:30 +01:00
|
|
|
}
|
|
|
|
|
2015-10-30 21:10:16 +01:00
|
|
|
/**
|
|
|
|
*
|
2015-10-28 23:54:30 +01:00
|
|
|
* Reflective accessor for Station podcast publication status for the
|
|
|
|
* file with the given ID
|
|
|
|
*
|
|
|
|
* @param int $fileId the ID of the file to check
|
|
|
|
*
|
2015-11-10 23:54:31 +01:00
|
|
|
* @return int 1 if the file has been published to SoundCloud,
|
|
|
|
* 0 if the file has yet to be published, or -1 if the
|
|
|
|
* file is in a pending state
|
2015-10-28 23:54:30 +01:00
|
|
|
*/
|
|
|
|
private static function getStationPodcastPublishStatus($fileId) {
|
|
|
|
$stationPodcast = StationPodcastQuery::create()
|
|
|
|
->findOneByDbPodcastId(Application_Model_Preference::getStationPodcastId());
|
2015-11-10 23:54:31 +01:00
|
|
|
return (int) $stationPodcast->hasEpisodeForFile($fileId);
|
2015-10-28 23:54:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|