-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
manage and documentation for file #3525
- Loading branch information
Showing
10 changed files
with
162 additions
and
52 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace App\Service\Files; | ||
|
||
use App\Service\ImageManipulationHandler; | ||
use League\Flysystem\FilesystemException; | ||
use League\Flysystem\FilesystemOperator; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
use Symfony\Component\HttpFoundation\File\File; | ||
|
||
readonly class ImageVariantProvider | ||
{ | ||
public function __construct( | ||
private FilesystemOperator $fileStorage, | ||
private ParameterBagInterface $parameterBag, | ||
) { | ||
} | ||
|
||
/** | ||
* @throws FilesystemException | ||
* @throws \Exception | ||
*/ | ||
public function getFileVariant(string $filename, ?string $variant = null): File | ||
{ | ||
$variantNames = ImageManipulationHandler::getVariantNames($filename); | ||
if ('thumb' === $variant && $this->fileStorage->fileExists($variantNames[ImageManipulationHandler::SUFFIX_THUMB])) { | ||
$filename = $variantNames[ImageManipulationHandler::SUFFIX_THUMB]; | ||
} elseif ($this->fileStorage->fileExists($variantNames[ImageManipulationHandler::SUFFIX_RESIZE])) { | ||
$filename = $variantNames[ImageManipulationHandler::SUFFIX_RESIZE]; | ||
} | ||
if (!$this->fileStorage->fileExists($filename)) { | ||
throw new \Exception('File "'.$filename.'" not found'); | ||
} | ||
$tmpFilepath = $this->parameterBag->get('uploads_tmp_dir').$filename; | ||
$bucketFilepath = $this->parameterBag->get('url_bucket').'/'.$filename; | ||
$content = file_get_contents($bucketFilepath); | ||
file_put_contents($tmpFilepath, $content); | ||
|
||
return new File($tmpFilepath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Tests\Unit\Service\Files; | ||
|
||
use App\Service\Files\ImageVariantProvider; | ||
use League\Flysystem\FilesystemException; | ||
use League\Flysystem\FilesystemOperator; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
|
||
class ImageVariantProviderTest extends KernelTestCase | ||
{ | ||
private FilesystemOperator|MockObject $fileStorageMock; | ||
private ImageVariantProvider|MockObject $imageVariantProvider; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->fileStorageMock = $this->createMock(FilesystemOperator::class); | ||
$parameterBag = self::getContainer()->get(ParameterBagInterface::class); | ||
|
||
$this->imageVariantProvider = new ImageVariantProvider( | ||
$this->fileStorageMock, | ||
$parameterBag | ||
); | ||
} | ||
|
||
/** | ||
* @throws FilesystemException | ||
* | ||
* @dataProvider provideImages | ||
*/ | ||
public function testGetFileVariantSuccessfullyRetrievesThumb(string $variant): void | ||
{ | ||
$this->fileStorageMock | ||
->method('fileExists') | ||
->willReturn(true); | ||
|
||
$file = $this->imageVariantProvider->getFileVariant(__DIR__.'/../../../files/sample.png', $variant); | ||
$this->assertStringContainsString($variant, $file->getFilename(), 'The filename should contain the variant'); | ||
} | ||
|
||
public function provideImages(): \Generator | ||
{ | ||
yield 'Sample for thumb' => ['thumb']; | ||
yield 'Sample for resize' => ['resize']; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
tests/Unit/Service/File/ZipHelperTest.php → tests/Unit/Service/Files/ZipHelperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters