-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/fileitem factory #362
base: 2.1
Are you sure you want to change the base?
Changes from all commits
7bd1b82
3fb696d
bbb5ab3
8f5ba75
1af134f
2aab579
c23b147
a31fb9d
e511089
c368842
e721a25
ec6345d
25db585
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?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; | ||
use Terminal42\NotificationCenterBundle\Exception\BulkyItem\InvalidFileItemException; | ||
|
||
class FileItemFactory | ||
{ | ||
public function __construct(private readonly MimeTypeGuesserInterface|null $mimeTypeGuesser = null) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws InvalidFileItemException if the information cannot be fetched automatically (e.g. missing mime type guesser service) | ||
*/ | ||
public function createFromLocalPath(string $path): FileItem | ||
{ | ||
if (!(new Filesystem())->exists($path)) { | ||
throw new InvalidFileItemException(\sprintf('The file "%s" does not exist.', $path)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we could use the Symfony Filesystem There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it's best practice to use domain exceptions. |
||
} | ||
|
||
$name = basename($path); | ||
$mimeType = (string) $this->mimeTypeGuesser?->guessMimeType($path); | ||
$size = (int) filesize($path); | ||
|
||
return FileItem::fromPath($path, $name, $mimeType, $size); | ||
} | ||
|
||
/** | ||
* @throws InvalidFileItemException | ||
*/ | ||
public function createFromVfsFilesystemItem(FilesystemItem $file, VirtualFilesystemInterface $virtualFilesystem): FileItem | ||
{ | ||
$stream = $virtualFilesystem->readStream($file->getPath()); | ||
|
||
return FileItem::fromStream($stream, $file->getName(), $file->getMimeType(), $file->getFileSize()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does whatever the implementation of the virtualfilesystem does. We don't care here. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Terminal42\NotificationCenterBundle\Exception\BulkyItem; | ||
|
||
class InvalidFileItemException extends \InvalidArgumentException | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Terminal42\NotificationCenterBundle\Test\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()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Terminal42\NotificationCenterBundle\Test\BulkyItem; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Terminal42\NotificationCenterBundle\BulkyItem\FileItem; | ||
use Terminal42\NotificationCenterBundle\Exception\BulkyItem\InvalidFileItemException; | ||
|
||
class FileItemTest extends TestCase | ||
{ | ||
public function testCannotCreateEmptyNameFileItem(): void | ||
{ | ||
$this->expectException(InvalidFileItemException::class); | ||
$this->expectExceptionMessage('Name must not be empty'); | ||
|
||
FileItem::fromPath(__DIR__.'/../Fixtures/name.jpg', '', 'image/jpg', 0); | ||
} | ||
|
||
public function testCannotCreateEmptyMimeTypeFileItem(): void | ||
{ | ||
$this->expectException(InvalidFileItemException::class); | ||
$this->expectExceptionMessage('Mime type must not be empty'); | ||
|
||
FileItem::fromPath(__DIR__.'/../Fixtures/name.jpg', 'name.jpg', '', 0); | ||
} | ||
|
||
public function testCannotCreateInvalidFileSizeFileItem(): void | ||
{ | ||
$this->expectException(InvalidFileItemException::class); | ||
$this->expectExceptionMessage('File size must not be smaller than 0'); | ||
|
||
FileItem::fromPath(__DIR__.'/../Fixtures/name.jpg', 'name.jpg', 'image/jpg', -42); | ||
} | ||
} |
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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both comments are correct, you would need to merge them in your suggestion.