sintonia/legacy/application/services/ThirdPartyService.php

111 lines
3.4 KiB
PHP
Raw Normal View History

<?php
/**
2021-10-11 16:10:47 +02:00
* Class ServiceNotFoundException.
*/
2021-10-11 16:10:47 +02:00
class ServiceNotFoundException extends Exception
{
}
/**
2021-10-11 16:10:47 +02:00
* Class ThirdPartyService generic superclass for third-party services.
*/
2021-10-11 16:10:47 +02:00
abstract class Application_Service_ThirdPartyService
{
/**
* @var string service name to store in ThirdPartyTrackReferences database
*/
protected static $_SERVICE_NAME;
/**
* Create a ThirdPartyTrackReferences object for a track that's been uploaded
* to an external service
* TODO: should we have a database layer class to handle Propel operations?
*
* @param $fileId int local CcFiles identifier
*
2022-09-12 13:16:14 +02:00
* @return string the new ThirdPartyTrackReferences identifier
*
* @throws Exception
* @throws PropelException
*/
2021-10-11 16:10:47 +02:00
public function createTrackReference($fileId)
{
// First, check if the track already has an entry in the database
// 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)
->findOneByDbFileId($fileId);
2015-11-27 00:54:49 +01:00
if (is_null($ref)) {
$ref = new ThirdPartyTrackReferences();
}
$ref->setDbService(static::$_SERVICE_NAME);
$ref->setDbFileId($fileId);
$ref->save();
2021-10-11 16:10:47 +02:00
return $ref->getDbId();
}
/**
* Remove a ThirdPartyTrackReferences row from the database.
* 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.
*
* @param $fileId int cc_files identifier
*
* @throws Exception
* @throws PropelException
*/
2021-10-11 16:10:47 +02:00
public function removeTrackReference($fileId)
{
$ref = ThirdPartyTrackReferencesQuery::create()
->filterByDbService(static::$_SERVICE_NAME)
->findOneByDbFileId($fileId);
$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.
*
* @param int $fileId the cc_files identifier
*
* @return string the service foreign identifier
*/
2021-10-11 16:10:47 +02:00
public function getServiceId($fileId)
{
$ref = ThirdPartyTrackReferencesQuery::create()
->filterByDbService(static::$_SERVICE_NAME)
2021-10-11 16:10:47 +02:00
->findOneByDbFileId($fileId) // There shouldn't be duplicates!
;
2021-10-11 16:10:47 +02:00
return empty($ref) ? '' : $ref->getDbForeignId();
}
/**
2021-10-11 16:10:47 +02:00
* Check if a reference exists for a given CcFiles identifier.
*
* @param int $fileId the cc_files identifier
*
* @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
*/
2021-10-11 16:10:47 +02:00
public function referenceExists($fileId)
{
$ref = ThirdPartyTrackReferencesQuery::create()
->filterByDbService(static::$_SERVICE_NAME)
->findOneByDbFileId($fileId);
if (!empty($ref)) {
$task = CeleryTasksQuery::create()
->orderByDbDispatchTime(Criteria::DESC)
->findOneByDbTrackReference($ref->getDbId());
2021-10-11 16:10:47 +02:00
return $task->getDbStatus() == CELERY_PENDING_STATUS ? -1
2022-07-07 20:01:15 +02:00
: ($task->getDbStatus() == CELERY_FAILED_STATUS ? 0 : 1);
}
2021-10-11 16:10:47 +02:00
return 0;
}
2021-10-11 16:10:47 +02:00
}