Skip to content

Commit

Permalink
Add event
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenRenaux authored and jmsche committed Dec 6, 2024
1 parent 7acaf0e commit 8f52e36
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"php": "^8.2",
"doctrine/orm": "^2.10 || ^3.0",
"symfony/deprecation-contracts": "^3.5",
"symfony/event-dispatcher": "^6.4 || ^7.0",
"symfony/form": "^6.4 || ^7.0",
"symfony/framework-bundle": "^6.4 || ^7.0",
"symfony/http-foundation": "^6.4 || ^7.0",
Expand Down
3 changes: 3 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

return static function (ContainerConfigurator $container): void {
$container->services()
// Controllers
Expand Down Expand Up @@ -78,6 +80,7 @@
->set(FileStorageManager::class)
->arg('$filesystemStorage', service(FilesystemStorage::class))
->arg('$flysystemStorage', service(FlysystemStorage::class))
->arg('$eventDispatcher', service(EventDispatcherInterface::class))

// Form types
->set(FileType::class)
Expand Down
17 changes: 17 additions & 0 deletions src/Event/UploadedFileEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Leapt\CoreBundle\Event;

use Leapt\CoreBundle\FileStorage\FileUploadConfig;
use Symfony\Contracts\EventDispatcher\Event;

final class UploadedFileEvent extends Event
{
public function __construct(
public \SplFileInfo $splFileInfo,
public FileUploadConfig $fileUploadConfig,
) {
}
}
5 changes: 5 additions & 0 deletions src/FileStorage/FileStorageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace Leapt\CoreBundle\FileStorage;

use Leapt\CoreBundle\Event\UploadedFileEvent;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

final class FileStorageManager
{
public function __construct(
private FilesystemStorage $filesystemStorage,
private FlysystemStorage $flysystemStorage,
private EventDispatcherInterface $eventDispatcher,
) {
}

Expand All @@ -21,6 +24,8 @@ public function uploadFile(FileUploadConfig $fileUploadConfig, File $uploadedFil
} else {
$this->filesystemStorage->uploadFile($fileUploadConfig, $uploadedFile, $path, $filename);
}

$this->eventDispatcher->dispatch(new UploadedFileEvent(new \SplFileInfo($path . \DIRECTORY_SEPARATOR . $filename), $fileUploadConfig));
}

public function removeFile(FileUploadConfig $fileUploadConfig, string $file): void
Expand Down
2 changes: 2 additions & 0 deletions tests/Listener/FileSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class FileSubscriberTest extends TestCase
{
Expand All @@ -46,6 +47,7 @@ protected function setUp(): void
$fileStorageManager = new FileStorageManager(
new FilesystemStorage($this->rootDir),
new FlysystemStorage([]),
$this->createMock(EventDispatcherInterface::class),
);
$this->subscriber = new FileSubscriber($fileStorageManager);

Expand Down

0 comments on commit 8f52e36

Please sign in to comment.