2015-06-03 22:57:17 +02:00
|
|
|
<?php
|
|
|
|
|
2015-06-15 21:12:37 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Class ServiceNotFoundException.
|
2015-06-15 21:12:37 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
class ServiceNotFoundException extends Exception
|
|
|
|
{
|
|
|
|
}
|
2015-06-15 21:12:37 +02:00
|
|
|
|
2015-06-03 22:57:17 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Class ThirdPartyService generic superclass for third-party services.
|
2015-06-03 22:57:17 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
abstract class Application_Service_ThirdPartyService
|
|
|
|
{
|
2015-06-03 22:57:17 +02:00
|
|
|
/**
|
|
|
|
* @var string service name to store in ThirdPartyTrackReferences database
|
|
|
|
*/
|
2015-06-15 21:12:37 +02:00
|
|
|
protected static $_SERVICE_NAME;
|
2015-06-03 22:57:17 +02:00
|
|
|
|
2015-06-10 21:04:49 +02:00
|
|
|
/**
|
2015-06-16 21:10:08 +02:00
|
|
|
* Create a ThirdPartyTrackReferences object for a track that's been uploaded
|
|
|
|
* to an external service
|
2015-06-10 23:11:42 +02:00
|
|
|
* TODO: should we have a database layer class to handle Propel operations?
|
2015-06-03 22:57:17 +02:00
|
|
|
*
|
2015-06-16 21:10:08 +02:00
|
|
|
* @param $fileId int local CcFiles identifier
|
|
|
|
*
|
2022-09-12 13:16:14 +02:00
|
|
|
* @return string the new ThirdPartyTrackReferences identifier
|
|
|
|
*
|
2015-06-03 22:57:17 +02:00
|
|
|
* @throws Exception
|
|
|
|
* @throws PropelException
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function createTrackReference($fileId)
|
|
|
|
{
|
2015-06-03 22:57:17 +02:00
|
|
|
// First, check if the track already has an entry in the database
|
2015-09-24 21:57:38 +02:00
|
|
|
// If the file ID given is null, create a new reference
|
2015-11-27 00:54:49 +01:00
|
|
|
$ref = is_null($fileId) ? null : ThirdPartyTrackReferencesQuery::create()
|
|
|
|
->filterByDbService(static::$_SERVICE_NAME)
|
2022-01-23 19:15:55 +01:00
|
|
|
->findOneByDbFileId($fileId);
|
2015-11-27 00:54:49 +01:00
|
|
|
if (is_null($ref)) {
|
|
|
|
$ref = new ThirdPartyTrackReferences();
|
|
|
|
}
|
2015-06-15 21:12:37 +02:00
|
|
|
$ref->setDbService(static::$_SERVICE_NAME);
|
2015-06-03 22:57:17 +02:00
|
|
|
$ref->setDbFileId($fileId);
|
|
|
|
$ref->save();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-06-16 21:10:08 +02:00
|
|
|
return $ref->getDbId();
|
2015-06-03 22:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-15 21:06:03 +02:00
|
|
|
* Remove a ThirdPartyTrackReferences row from the database.
|
2015-06-03 22:57:17 +02:00
|
|
|
* This is necessary if the track was removed from the service
|
2021-10-11 16:10:47 +02:00
|
|
|
* or the foreign id in our database is incorrect.
|
2015-06-03 22:57:17 +02:00
|
|
|
*
|
2015-06-16 21:10:08 +02:00
|
|
|
* @param $fileId int cc_files identifier
|
2015-06-03 22:57:17 +02:00
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
* @throws PropelException
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function removeTrackReference($fileId)
|
|
|
|
{
|
2015-06-03 22:57:17 +02:00
|
|
|
$ref = ThirdPartyTrackReferencesQuery::create()
|
2015-06-15 21:12:37 +02:00
|
|
|
->filterByDbService(static::$_SERVICE_NAME)
|
2022-01-23 19:15:55 +01:00
|
|
|
->findOneByDbFileId($fileId);
|
2015-06-03 22:57:17 +02:00
|
|
|
$ref->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a CcFiles identifier for a file that's been uploaded to a third-party service,
|
2021-10-11 16:10:47 +02:00
|
|
|
* return the third-party identifier for the remote file.
|
2015-06-03 22:57:17 +02:00
|
|
|
*
|
2015-06-19 00:18:48 +02:00
|
|
|
* @param int $fileId the cc_files identifier
|
2015-06-03 22:57:17 +02:00
|
|
|
*
|
2015-06-16 21:10:08 +02:00
|
|
|
* @return string the service foreign identifier
|
2015-06-03 22:57:17 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function getServiceId($fileId)
|
|
|
|
{
|
2015-06-03 22:57:17 +02:00
|
|
|
$ref = ThirdPartyTrackReferencesQuery::create()
|
2015-06-15 21:12:37 +02:00
|
|
|
->filterByDbService(static::$_SERVICE_NAME)
|
2021-10-11 16:10:47 +02:00
|
|
|
->findOneByDbFileId($fileId) // There shouldn't be duplicates!
|
2022-01-23 19:15:55 +01:00
|
|
|
;
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-06-16 21:10:08 +02:00
|
|
|
return empty($ref) ? '' : $ref->getDbForeignId();
|
2015-06-03 22:57:17 +02:00
|
|
|
}
|
|
|
|
|
2015-06-19 00:18:48 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Check if a reference exists for a given CcFiles identifier.
|
2015-06-19 00:18:48 +02:00
|
|
|
*
|
|
|
|
* @param int $fileId the cc_files identifier
|
|
|
|
*
|
2015-11-10 23:54:31 +01:00
|
|
|
* @return int 1 if the file has been published,
|
|
|
|
* 0 if the file has yet to be published,
|
|
|
|
* or -1 if the file is in a pending state
|
2015-06-19 00:18:48 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function referenceExists($fileId)
|
|
|
|
{
|
2015-06-19 00:18:48 +02:00
|
|
|
$ref = ThirdPartyTrackReferencesQuery::create()
|
|
|
|
->filterByDbService(static::$_SERVICE_NAME)
|
2022-01-23 19:15:55 +01:00
|
|
|
->findOneByDbFileId($fileId);
|
2015-06-24 01:02:28 +02:00
|
|
|
if (!empty($ref)) {
|
|
|
|
$task = CeleryTasksQuery::create()
|
2015-11-10 23:54:31 +01:00
|
|
|
->orderByDbDispatchTime(Criteria::DESC)
|
2022-01-23 19:15:55 +01:00
|
|
|
->findOneByDbTrackReference($ref->getDbId());
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-11-10 23:54:31 +01:00
|
|
|
return $task->getDbStatus() == CELERY_PENDING_STATUS ? -1
|
2022-07-07 20:01:15 +02:00
|
|
|
: ($task->getDbStatus() == CELERY_FAILED_STATUS ? 0 : 1);
|
2015-06-24 01:02:28 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-11-10 23:54:31 +01:00
|
|
|
return 0;
|
2015-06-19 00:18:48 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
}
|