Skip to content

Commit

Permalink
Added a FileItemFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Sep 17, 2024
1 parent a31fb9d commit e511089
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 25 deletions.
7 changes: 7 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Contracts\Translation\TranslatorInterface;
use Terminal42\NotificationCenterBundle\Backend\AutoSuggester;
use Terminal42\NotificationCenterBundle\BulkyItem\BulkyItemStorage;
use Terminal42\NotificationCenterBundle\BulkyItem\FileItemFactory;
use Terminal42\NotificationCenterBundle\Config\ConfigLoader;
use Terminal42\NotificationCenterBundle\Cron\PruneBulkyItemStorageCron;
use Terminal42\NotificationCenterBundle\DependencyInjection\Terminal42NotificationCenterExtension;
Expand Down Expand Up @@ -59,6 +60,12 @@
])
;

$services->set(FileItemFactory::class)
->args([
service('mime_types'),
])
;

$services->set(PruneBulkyItemStorageCron::class)
->args([
service(BulkyItemStorage::class),
Expand Down
37 changes: 37 additions & 0 deletions src/BulkyItem/FileItemFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Terminal42\NotificationCenterBundle\BulkyItem;

use Contao\CoreBundle\Filesystem\FilesystemItem;
use Contao\CoreBundle\Filesystem\VirtualFilesystemInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Mime\MimeTypeGuesserInterface;

class FileItemFactory
{
public function __construct(private readonly MimeTypeGuesserInterface $mimeTypeGuesser)
{
}

public function createFromLocalPath(string $path): FileItem
{
if (!(new Filesystem())->exists($path)) {
throw new \InvalidArgumentException(\sprintf('The file "%s" does not exist.', $path));
}

$name = basename($path);
$mimeType = (string) $this->mimeTypeGuesser->guessMimeType($path);
$size = (int) filesize($path);

return FileItem::fromPath($path, $name, $mimeType, $size);
}

public function createFromVfsFilesystemItem(FilesystemItem $file, VirtualFilesystemInterface $virtualFilesystem): FileItem
{
$stream = $virtualFilesystem->readStream($file->getPath());

return FileItem::fromStream($stream, $file->getName(), $file->getMimeType(), $file->getFileSize());
}
}
41 changes: 41 additions & 0 deletions tests/BulkyItem/FileItemFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace BulkyItem;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mime\MimeTypes;
use Terminal42\NotificationCenterBundle\BulkyItem\FileItemFactory;
use Terminal42\NotificationCenterBundle\Test\VirtualFilesystemTestTrait;

class FileItemFactoryTest extends TestCase
{
use VirtualFilesystemTestTrait;

public function testCreateFromLocalPath(): void
{
$factory = new FileItemFactory(new MimeTypes());
$item = $factory->createFromLocalPath(__DIR__.'/../Fixtures/name.jpg');
$this->assertSame('name.jpg', $item->getName());
$this->assertSame('image/jpeg', $item->getMimeType());
$this->assertSame(333, $item->getSize());
$this->assertIsResource($item->getContents());
}

public function testCreateFromVfsFilesystemItem(): void
{
$vfsCollection = $this->createVfsCollection();
$vfs = $vfsCollection->get('files');
$vfs->write('media/name.jpg', file_get_contents(__DIR__.'/../Fixtures/name.jpg'));

$item = $vfs->get('media/name.jpg');

$factory = new FileItemFactory(new MimeTypes());
$item = $factory->createFromVfsFilesystemItem($item, $vfs);
$this->assertSame('name.jpg', $item->getName());
$this->assertSame('image/jpeg', $item->getMimeType());
$this->assertSame(333, $item->getSize());
$this->assertIsResource($item->getContents());
}
}
28 changes: 3 additions & 25 deletions tests/Gateway/MailerGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
namespace Terminal42\NotificationCenterBundle\Test\Gateway;

use Contao\Controller;
use Contao\CoreBundle\Filesystem\Dbafs\DbafsManager;
use Contao\CoreBundle\Filesystem\MountManager;
use Contao\CoreBundle\Filesystem\VirtualFilesystem;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\String\SimpleTokenParser;
use Contao\FrontendTemplate;
use Contao\TestCase\ContaoTestCase;
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Mailer\MailerInterface;
Expand All @@ -25,13 +21,14 @@
use Terminal42\NotificationCenterBundle\Parcel\Parcel;
use Terminal42\NotificationCenterBundle\Parcel\Stamp\LanguageConfigStamp;
use Terminal42\NotificationCenterBundle\Parcel\Stamp\TokenCollectionStamp;
use Terminal42\NotificationCenterBundle\Test\BulkyItem\InMemoryDbafs;
use Terminal42\NotificationCenterBundle\Test\BulkyItem\VirtualFilesystemCollection;
use Terminal42\NotificationCenterBundle\Test\VirtualFilesystemTestTrait;
use Terminal42\NotificationCenterBundle\Token\Token;
use Terminal42\NotificationCenterBundle\Token\TokenCollection;

class MailerGatewayTest extends ContaoTestCase
{
use VirtualFilesystemTestTrait;

/**
* @dataProvider embeddingHtmlImagesProvider
*
Expand Down Expand Up @@ -122,25 +119,6 @@ public static function embeddingHtmlImagesProvider(): iterable
];
}

private function createVfsCollection(): VirtualFilesystemCollection
{
$mountManager = (new MountManager())
->mount(new InMemoryFilesystemAdapter(), 'files')
->mount(new InMemoryFilesystemAdapter(), 'bulky_item')
;

$dbafsManager = new DbafsManager();
$dbafsManager->register(new InMemoryDbafs(), 'files');
$dbafsManager->register(new InMemoryDbafs(), 'bulky_item');

$vfsCollection = new VirtualFilesystemCollection();
$vfsCollection->add(new VirtualFilesystem($mountManager, $dbafsManager, 'files'));
$vfsCollection->add(new VirtualFilesystem($mountManager, $dbafsManager, 'bulky_item'));
$vfsCollection->add(new VirtualFilesystem($mountManager, $dbafsManager, '')); // Global one

return $vfsCollection;
}

private function createFrameWorkWithTemplate(string $parsedTemplateHtml): ContaoFramework
{
$controllerAdapter = $this->mockAdapter(['convertRelativeUrls']);
Expand Down
34 changes: 34 additions & 0 deletions tests/VirtualFilesystemTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Terminal42\NotificationCenterBundle\Test;

use Contao\CoreBundle\Filesystem\Dbafs\DbafsManager;
use Contao\CoreBundle\Filesystem\MountManager;
use Contao\CoreBundle\Filesystem\VirtualFilesystem;
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
use Terminal42\NotificationCenterBundle\Test\BulkyItem\InMemoryDbafs;
use Terminal42\NotificationCenterBundle\Test\BulkyItem\VirtualFilesystemCollection;

trait VirtualFilesystemTestTrait
{
private function createVfsCollection(): VirtualFilesystemCollection
{
$mountManager = (new MountManager())
->mount(new InMemoryFilesystemAdapter(), 'files')
->mount(new InMemoryFilesystemAdapter(), 'bulky_item')
;

$dbafsManager = new DbafsManager();
$dbafsManager->register(new InMemoryDbafs(), 'files');
$dbafsManager->register(new InMemoryDbafs(), 'bulky_item');

$vfsCollection = new VirtualFilesystemCollection();
$vfsCollection->add(new VirtualFilesystem($mountManager, $dbafsManager, 'files'));
$vfsCollection->add(new VirtualFilesystem($mountManager, $dbafsManager, 'bulky_item'));
$vfsCollection->add(new VirtualFilesystem($mountManager, $dbafsManager, '')); // Global one

return $vfsCollection;
}
}

0 comments on commit e511089

Please sign in to comment.