Skip to content

Commit

Permalink
Refactor AddFolder case
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Jan 4, 2024
1 parent a001963 commit 7068cf1
Show file tree
Hide file tree
Showing 22 changed files with 237 additions and 175 deletions.
2 changes: 1 addition & 1 deletion assets/out/src/js/base.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/out/src/js/base.min.js.map

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/Application/Controller/Admin/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OxidEsales\MediaLibrary\Breadcrumb\Service\BreadcrumbServiceInterface;
use OxidEsales\MediaLibrary\Image\Service\ImageResourceInterface;
use OxidEsales\MediaLibrary\Media\Repository\MediaRepositoryInterface;
use OxidEsales\MediaLibrary\Service\FolderServiceInterface;
use OxidEsales\MediaLibrary\Service\Media;
use OxidEsales\MediaLibrary\Transput\RequestData\AddFolderRequestInterface;
use OxidEsales\MediaLibrary\Transput\RequestData\UIRequestInterface;
Expand Down Expand Up @@ -131,12 +132,12 @@ public function addFolder(): void

$responseData = [];
if ($folderName = $addFolderRequest->getName()) {
$mediaService = $this->getService(Media::class);
$newDirectoryInformation = $mediaService->createCustomDir($folderName);
$folderService = $this->getService(FolderServiceInterface::class);
$newDirectoryInformation = $folderService->createCustomDir($folderName);

$responseData = [
'id' => $newDirectoryInformation['id'],
'name' => $newDirectoryInformation['dir']
'id' => $newDirectoryInformation->getOxid(),
'name' => $newDirectoryInformation->getFileName()
];
}

Expand Down
30 changes: 30 additions & 0 deletions src/Image/Service/ImageResourceRefactored.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

namespace OxidEsales\MediaLibrary\Image\Service;

use OxidEsales\Eshop\Core\Config;
use Symfony\Component\Filesystem\Path;

class ImageResourceRefactored implements ImageResourceRefactoredInterface
{
public const MEDIA_PATH = 'out/pictures/ddmedia';

public function __construct(
protected Config $shopConfig,
) {
}

public function getPathToMediaFiles(string $subDirectory = ''): string
{
return Path::join(
$this->shopConfig->getConfigParam('sShopDir'),
self::MEDIA_PATH,
$subDirectory
);
}
}
15 changes: 15 additions & 0 deletions src/Image/Service/ImageResourceRefactoredInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\MediaLibrary\Image\Service;

interface ImageResourceRefactoredInterface
{
public function getPathToMediaFiles(string $subDirectory = ''): string;
}
4 changes: 4 additions & 0 deletions src/Image/Service/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ services:

OxidEsales\MediaLibrary\Image\Service\ImageResourceInterface:
class: OxidEsales\MediaLibrary\Image\Service\ImageResource
public: true

OxidEsales\MediaLibrary\Image\Service\ImageResourceRefactoredInterface:
class: OxidEsales\MediaLibrary\Image\Service\ImageResourceRefactored
public: true
36 changes: 34 additions & 2 deletions src/Service/FolderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,39 @@

namespace OxidEsales\MediaLibrary\Service;

class FolderService
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use OxidEsales\MediaLibrary\Image\Service\ImageResourceRefactoredInterface;
use OxidEsales\MediaLibrary\Media\DataType\Media as MediaDataType;
use OxidEsales\MediaLibrary\Media\Repository\MediaRepositoryInterface;

class FolderService implements FolderServiceInterface
{
public function __construct(
protected ImageResourceRefactoredInterface $imageResource,
protected NamingServiceInterface $namingService,
protected MediaRepositoryInterface $mediaRepository,
protected FileSystemServiceInterface $fileSystemService,
protected ShopAdapterInterface $shopAdapter,
) {
}

public function createCustomDir(string $folderName): MediaDataType
{
$folderName = $this->namingService->sanitizeFilename($folderName);

$folderPath = $this->imageResource->getPathToMediaFiles($folderName);
$folderPath = $this->namingService->getUniqueFilename($folderPath);

$this->fileSystemService->ensureDirectory($folderPath);

$newMedia = new MediaDataType(
oxid: $this->shopAdapter->generateUniqueId(),
fileName: basename($folderPath),
fileType: 'directory'
);

$this->mediaRepository->addMedia($newMedia);

}
return $newMedia;
}
}
17 changes: 17 additions & 0 deletions src/Service/FolderServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\MediaLibrary\Service;

use OxidEsales\MediaLibrary\Media\DataType\Media as MediaDataType;

interface FolderServiceInterface
{
public function createCustomDir(string $folderName): MediaDataType;
}
38 changes: 3 additions & 35 deletions src/Service/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OxidEsales\Eshop\Core\UtilsObject;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
use OxidEsales\MediaLibrary\Image\DataTransfer\ImageSize;
use OxidEsales\MediaLibrary\Image\Service\ImageResourceRefactoredInterface;
use OxidEsales\MediaLibrary\Image\Service\ThumbnailGeneratorInterface;
use OxidEsales\MediaLibrary\Image\Service\ImageResourceInterface;
use OxidEsales\MediaLibrary\Media\DataType\Media as MediaDataType;
Expand All @@ -35,7 +36,8 @@ public function __construct(
public ImageResourceInterface $imageResource,
protected NamingServiceInterface $namingService,
protected MediaRepositoryInterface $mediaRepository,
private FileSystemServiceInterface $fileSystemService
private FileSystemServiceInterface $fileSystemService,
protected FolderServiceInterface $folderService
) {
$this->connection = $connectionProvider->get();
}
Expand Down Expand Up @@ -111,40 +113,6 @@ public function createDirs()
$this->fileSystemService->ensureDirectory($this->imageResource->getThumbnailPath());
}

public function createCustomDir($sName)
{
$this->createDirs();

$sPath = $this->imageResource->getMediaPath();
$sNewPath = $sPath . $sName;

$sNewPath = $this->namingService->getUniqueFilename($sNewPath);

$this->fileSystemService->ensureDirectory($sNewPath);

// todo: before adding entry into db the existence should be checked

$sFolderName = basename($sNewPath);

$sSelect = "SELECT OXID FROM `ddmedia` WHERE `DDFILENAME` = ? AND `DDFILETYPE` = ?";
$sId = $this->connection->fetchOne($sSelect, [$sFolderName, 'directory']);

if (!$sId) {
$sId = $this->generateUId();

$newMedia = new MediaDataType(
oxid: $sId,
fileName: $sFolderName,
fileType: 'directory'
);

$this->mediaRepository->addMedia($newMedia);
}


return ['id' => $sId, 'dir' => $sFolderName];
}

public function rename($sOldName, $sNewName, $sId, $sType = 'file')
{
$aResult = [
Expand Down
4 changes: 4 additions & 0 deletions src/Service/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ services:
class: OxidEsales\MediaLibrary\Service\FileSystemService
public: true

OxidEsales\MediaLibrary\Service\FolderServiceInterface:
class: OxidEsales\MediaLibrary\Service\FolderService
public: true

OxidEsales\MediaLibrary\Service\Media:
class: OxidEsales\MediaLibrary\Service\Media
public: true
Expand Down
2 changes: 1 addition & 1 deletion src/Transput/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ public function getIntRequestParameter(string $name): int
$value = $this->request->getRequestParameter($name);
return (int)$value;
}
}
}
2 changes: 1 addition & 1 deletion src/Transput/RequestData/AddFolderRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ public function getName(): string
{
return $this->request->getStringRequestParameter(self::REQUEST_PARAM_NAME);
}
}
}
2 changes: 1 addition & 1 deletion src/Transput/RequestData/AddFolderRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
interface AddFolderRequestInterface
{
public function getName(): string;
}
}
2 changes: 1 addition & 1 deletion src/Transput/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ public function getBoolRequestParameter(string $name): bool;
public function getStringRequestParameter(string $name, string $default = ''): string;

public function getIntRequestParameter(string $name): int;
}
}
13 changes: 9 additions & 4 deletions tests/Integration/Service/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OxidEsales\MediaLibrary\Image\Service\ImageResource;
use OxidEsales\MediaLibrary\Media\Repository\MediaRepositoryInterface;
use OxidEsales\MediaLibrary\Service\FileSystemServiceInterface;
use OxidEsales\MediaLibrary\Service\FolderServiceInterface;
use OxidEsales\MediaLibrary\Service\ModuleSettings;
use OxidEsales\MediaLibrary\Service\NamingServiceInterface;
use OxidEsales\MediaLibrary\Tests\Integration\IntegrationTestCase;
Expand Down Expand Up @@ -98,9 +99,12 @@ public function testUploadMediaInFolder($imageName, $destFileName, $fileType)
{
$sut = $this->getSut();

$aResult = $sut->createCustomDir(self::FIXTURE_FOLDER);
$sFolderId = $aResult['id'];
$sFolderName = $aResult['dir'];
/** @var FolderServiceInterface $folderService */
$folderService = $this->get(FolderServiceInterface::class);
$folderData = $folderService->createCustomDir(self::FIXTURE_FOLDER);

$sFolderId = $folderData->getOxid();
$sFolderName = $folderData->getFileName();

$this->assertNotEmpty($sFolderId);

Expand Down Expand Up @@ -229,7 +233,8 @@ protected function getSut(
$imageResourceMock,
namingService: $namingService ?: $this->containerFactory->get(NamingServiceInterface::class),
mediaRepository: $this->containerFactory->get(MediaRepositoryInterface::class),
fileSystemService: $this->containerFactory->get(FileSystemServiceInterface::class)
fileSystemService: $this->containerFactory->get(FileSystemServiceInterface::class),
folderService: $this->containerFactory->get(FolderServiceInterface::class)
);
}

Expand Down
16 changes: 12 additions & 4 deletions tests/Unit/Application/Controller/Admin/MediaControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace Application\Controller\Admin;

use OxidEsales\MediaLibrary\Application\Controller\Admin\MediaController;
use OxidEsales\MediaLibrary\Media\DataType\Media as MediaDataType;
use OxidEsales\MediaLibrary\Service\FolderServiceInterface;
use OxidEsales\MediaLibrary\Service\Media;
use OxidEsales\MediaLibrary\Transput\RequestData\AddFolderRequestInterface;
use OxidEsales\MediaLibrary\Transput\ResponseInterface;
Expand All @@ -27,8 +29,14 @@ public function testAddFolderSuccess(): void
$requestStub = $this->createStub(AddFolderRequestInterface::class);
$requestStub->method('getName')->willReturn($folderName);

$mediaStub = $this->createStub(Media::class);
$mediaStub->method('createCustomDir')->with($folderName)->willReturn(['id' => 'fid', 'dir' => 'someDirName']);
$newMediaItem = new MediaDataType(
oxid: 'fid',
fileName: 'someDirName',
fileType: 'directory'
);

$folderServiceStub = $this->createStub(FolderServiceInterface::class);
$folderServiceStub->method('createCustomDir')->with($folderName)->willReturn($newMediaItem);

$responseSpy = $this->createMock(ResponseInterface::class);
$responseSpy->expects($this->once())->method('responseAsJson')->with([
Expand All @@ -39,10 +47,10 @@ public function testAddFolderSuccess(): void
$sut = $this->createPartialMock(MediaController::class, ['getService']);
$sut->method('getService')->willReturnMap([
[AddFolderRequestInterface::class, $requestStub],
[Media::class, $mediaStub],
[FolderServiceInterface::class, $folderServiceStub],
[ResponseInterface::class, $responseSpy],
]);

$sut->addFolder();
}
}
}
50 changes: 50 additions & 0 deletions tests/Unit/Image/Service/ImageResourceRefactoredTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

namespace Image\Service;

use OxidEsales\Eshop\Core\Config;
use OxidEsales\MediaLibrary\Image\Service\ImageResourceRefactored;
use PHPUnit\Framework\TestCase;

/**
* @covers \OxidEsales\MediaLibrary\Image\Service\ImageResourceRefactored
*/
class ImageResourceRefactoredTest extends TestCase
{
public function testGetPathToMediaFiles(): void
{
$sut = $this->getSut(
shopConfig: $shopConfigStub = $this->createStub(Config::class)
);
$shopConfigStub->method('getConfigParam')->with('sShopDir')->willReturn('someShopDir');

$this->assertSame('someShopDir/' . ImageResourceRefactored::MEDIA_PATH, $sut->getPathToMediaFiles());
}

public function testGetPathToMediaFilesWithSubdirectory(): void
{
$sut = $this->getSut(
shopConfig: $shopConfigStub = $this->createStub(Config::class)
);
$shopConfigStub->method('getConfigParam')->with('sShopDir')->willReturn('someShopDir');

$subDirectory = '/some/sub/directory';
$this->assertSame(
'someShopDir/' . ImageResourceRefactored::MEDIA_PATH . $subDirectory,
$sut->getPathToMediaFiles($subDirectory)
);
}

protected function getSut(
Config $shopConfig = null,
) {
return new ImageResourceRefactored(
$shopConfig ?: $this->createStub(Config::class),
);
}
}
Loading

0 comments on commit 7068cf1

Please sign in to comment.