Skip to content

Commit

Permalink
Refactor notify controller and add new events
Browse files Browse the repository at this point in the history
  • Loading branch information
Randy Čupić committed Sep 19, 2024
1 parent b55a167 commit 805430d
Show file tree
Hide file tree
Showing 3 changed files with 709 additions and 37 deletions.
129 changes: 105 additions & 24 deletions bundle/Controller/Callback/Cloudinary/Notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Cloudinary\Api\Upload\UploadApi;
use Doctrine\ORM\EntityManagerInterface;
use Netgen\RemoteMedia\API\ProviderInterface;
use Netgen\RemoteMedia\API\Values\Folder;
use Netgen\RemoteMedia\API\Values\RemoteResource;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CacheableGatewayInterface;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryProvider;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryRemoteId;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\GatewayInterface;
use Netgen\RemoteMedia\Core\RequestVerifierInterface;
Expand All @@ -31,35 +33,23 @@ final class Notify extends AbstractController
{
private const RESOURCE_UPLOAD = 'upload';
private const RESOURCE_DELETE = 'delete';
private const RESOURCE_MOVE = 'move';
private const RESOURCE_TAGS_CHANGED = 'resource_tags_changed';
private const RESOURCE_CONTEXT_CHANGED = 'resource_context_changed';
private const RESOURCE_RENAME = 'rename';
private const RESOURCE_DISPLAY_NAME_CHANGED = 'resource_display_name_changed';
private const FOLDER_CREATE = 'create_folder';
private const FOLDER_DELETE = 'delete_folder';

private GatewayInterface $gateway;

private ProviderInterface $provider;

private RequestVerifierInterface $signatureVerifier;

private EntityManagerInterface $entityManager;

private EventDispatcherInterface $eventDispatcher;
private const FOLDER_MOVE_RENAME = 'move_or_rename_asset_folder';

public function __construct(
GatewayInterface $gateway,
ProviderInterface $provider,
RequestVerifierInterface $signatureVerifier,
EntityManagerInterface $entityManager,
EventDispatcherInterface $eventDispatcher,
) {
$this->gateway = $gateway;
$this->provider = $provider;
$this->signatureVerifier = $signatureVerifier;
$this->entityManager = $entityManager;
$this->eventDispatcher = $eventDispatcher;
}
private GatewayInterface $gateway,
private ProviderInterface $provider,
private RequestVerifierInterface $signatureVerifier,
private EntityManagerInterface $entityManager,
private EventDispatcherInterface $eventDispatcher,
private string $folderMode,
) {}

public function __invoke(Request $request): Response
{
Expand All @@ -84,6 +74,11 @@ public function __invoke(Request $request): Response

break;

case self::RESOURCE_MOVE:
$this->handleResourceMoved($requestContent);

break;

case self::RESOURCE_TAGS_CHANGED:
$this->handleTagsChanged($requestContent);

Expand All @@ -99,8 +94,14 @@ public function __invoke(Request $request): Response

break;

case self::RESOURCE_DISPLAY_NAME_CHANGED:
$this->handleDisplayNameChanged($requestContent);

break;

case self::FOLDER_CREATE:
case self::FOLDER_DELETE:
case self::FOLDER_MOVE_RENAME:
$this->handleFoldersChanged();

break;
Expand Down Expand Up @@ -140,7 +141,7 @@ private function handleResourceUploaded(array $requestContent): void

$resource
->setUrl($this->gateway->getDownloadLink($cloudinaryRemoteId))
->setName(pathinfo($cloudinaryRemoteId->getResourceId(), PATHINFO_FILENAME))
->setName($this->resolveName($requestContent))
->setVersion((string) $requestContent['version'])
->setSize($requestContent['bytes'])
->setTags($requestContent['tags']);
Expand Down Expand Up @@ -176,6 +177,42 @@ private function handleResourceDeleted(array $requestContent): void
}
}

private function handleResourceMoved(array $requestContent): void
{
if ($this->folderMode !== CloudinaryProvider::FOLDER_MODE_DYNAMIC) {
return;
}

if ($this->gateway instanceof CacheableGatewayInterface) {
$this->gateway->invalidateResourceListCache();
$this->gateway->invalidateFoldersCache();
}

foreach ($requestContent['resources'] ?? [] as $publicId => $resourceData) {
$cloudinaryRemoteId = new CloudinaryRemoteId(
$resourceData['type'],
$resourceData['resource_type'],
(string) $publicId,
);

$this->gateway->invalidateResourceCache($cloudinaryRemoteId);

try {
$resource = $this->provider->loadByRemoteId($cloudinaryRemoteId->getRemoteId());
} catch (RemoteResourceNotFoundException $e) {
continue;
}

$resource->setFolder(Folder::fromPath($resourceData['to_asset_folder']));

if (($resourceData['display_name'] ?? null) !== null) {
$resource->setName($resourceData['display_name']);
}

$this->provider->store($resource);
}
}

/**
* This method is a bit hacky due to inconsistent Cloudinary API response.
*/
Expand Down Expand Up @@ -216,7 +253,7 @@ private function handleResourceRenamed(array $requestContent): void

$resource
->setRemoteId($cloudinaryRemoteId->getRemoteId())
->setName(pathinfo($cloudinaryRemoteId->getResourceId(), PATHINFO_FILENAME))
->setName($this->resolveName($requestContent))
->setUrl($this->gateway->getDownloadLink($cloudinaryRemoteId))
->setFolder($cloudinaryRemoteId->getFolder());

Expand All @@ -234,6 +271,41 @@ private function handleResourceRenamed(array $requestContent): void
}
}

private function handleDisplayNameChanged(array $requestContent): void
{
if ($this->folderMode !== CloudinaryProvider::FOLDER_MODE_DYNAMIC) {
return;
}

if ($this->gateway instanceof CacheableGatewayInterface) {
$this->gateway->invalidateResourceListCache();
}

foreach ($requestContent['resources'] ?? [] as $resourceData) {
$cloudinaryRemoteId = new CloudinaryRemoteId(
$resourceData['type'],
$resourceData['resource_type'],
(string) $resourceData['public_id'],
);

if ($this->gateway instanceof CacheableGatewayInterface) {
$this->gateway->invalidateResourceCache($cloudinaryRemoteId);
}

try {
$resource = $this->provider->loadByRemoteId(
$cloudinaryRemoteId->getRemoteId(),
);
} catch (RemoteResourceNotFoundException $e) {
continue;
}

$resource->setName($resourceData['new_display_name']);

$this->provider->store($resource);
}
}

private function handleTagsChanged(array $requestContent): void
{
if ($this->gateway instanceof CacheableGatewayInterface) {
Expand Down Expand Up @@ -382,4 +454,13 @@ private function handleFoldersChanged(): void
$this->gateway->invalidateFoldersCache();
}
}

private function resolveName(array $data): string
{
$cloudinaryRemoteId = CloudinaryRemoteId::fromCloudinaryData($data);

return $this->folderMode === CloudinaryProvider::FOLDER_MODE_FIXED
? pathinfo($cloudinaryRemoteId->getResourceId(), PATHINFO_FILENAME)
: $data['display_name'] ?? pathinfo($cloudinaryRemoteId->getResourceId(), PATHINFO_FILENAME);
}
}
1 change: 1 addition & 0 deletions bundle/Resources/config/services/controllers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ services:
- '@netgen_remote_media.provider.cloudinary.verifier.controller.signature'
- '@doctrine.orm.entity_manager'
- '@event_dispatcher'
- '%netgen_remote_media.cloudinary.folder_mode%'
calls:
- [setContainer, ['@service_container']]
Loading

0 comments on commit 805430d

Please sign in to comment.