Skip to content

Commit

Permalink
Refactor and remove the _checkAndGetFolderName method
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Dec 29, 2023
1 parent c76beeb commit 2a26f7f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
37 changes: 2 additions & 35 deletions src/Service/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function createCustomDir($sName)
$sPath = $this->imageResource->getMediaPath();
$sNewPath = $sPath . $sName;

$sNewPath = $this->_checkAndGetFolderName($sNewPath, $sPath);
$sNewPath = $this->namingService->getUniqueFilename($sNewPath);

if (!is_dir($sNewPath)) {
mkdir($sNewPath);
Expand Down Expand Up @@ -198,11 +198,7 @@ public function rename($sOldName, $sNewName, $sId, $sType = 'file')
$sNewPath = $sPath . $sNewName;

$blDirectory = $sType == 'directory';
if ($blDirectory) {
$sNewPath = $this->_checkAndGetFolderName($sNewPath, $sPath);
} else {
$sNewPath = $this->namingService->getUniqueFilename($sNewPath);
}
$sNewPath = $this->namingService->getUniqueFilename($sNewPath);

$sOldThumbHash = $sNewThumbHash = $sNewThumbName = '';
if (!$blDirectory) {
Expand Down Expand Up @@ -325,35 +321,6 @@ public function moveFileToFolder($sSourceFileID, $sTargetFolderID)
return $blReturn;
}

/**
* @param $sNewPath
* @param $sPath
*
* @return string
*/
protected function _checkAndGetFolderName($sNewPath, $sPath)
{
while (file_exists($sNewPath)) {
$sBaseName = basename($sNewPath);

$aBaseParts = explode('_', $sBaseName);
$aBaseParts = array_reverse($aBaseParts);

$iFileCount = 0;
if (strlen($aBaseParts[0]) && is_numeric($aBaseParts[0])) {
$iFileCount = (int)$aBaseParts[0];
unset($aBaseParts[0]);
}

$sBaseName = implode('_', array_reverse($aBaseParts));

$sFileName = $sBaseName . '_' . (++$iFileCount);
$sNewPath = $sPath . $sFileName;
}

return $sNewPath;
}

/**
* @param $sNewName
*
Expand Down
8 changes: 5 additions & 3 deletions src/Service/NamingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ private function findNextPossibleFilename(string $path): string
$pathInfo = pathinfo($path);

if (preg_match('/(?P<baseFilename>.+)_(?P<numericPart>[0-9]+)$/', $pathInfo['filename'], $matches)) {
$fileName = $matches['baseFilename'] . '_' . ++$matches['numericPart'];
$newFileName = $matches['baseFilename'] . '_' . ++$matches['numericPart'];
} else {
$fileName = $pathInfo['filename'] . '_1';
$newFileName = $pathInfo['filename'] . '_1';
}

return $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $fileName . '.' . $pathInfo['extension'];
return $pathInfo['dirname']
. DIRECTORY_SEPARATOR . $newFileName
. ($pathInfo['extension'] ? '.' . $pathInfo['extension'] : '');
}
}
4 changes: 3 additions & 1 deletion tests/Unit/Service/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public function testCreateFolderWithNewName()
shopConfig: $shopConfigMock,
connectionProvider: $connectionProviderStub,
utilsObject: $utilsObjectMock,
mediaRepository: $mediaRepositoryMock,
namingService: $this->createPartialMock(NamingService::class, []),
mediaRepository: $mediaRepositoryMock
);
$aCustomDir = $sut->createCustomDir('FolderTest', '');

Expand Down Expand Up @@ -130,6 +131,7 @@ public function testCreateFolderWithExistingName()
shopConfig: $shopConfigMock,
connectionProvider: $connectionProviderStub,
utilsObject: $utilsObjectMock,
namingService: $this->createPartialMock(NamingService::class, [])
);
$aCustomDir = $sut->createCustomDir('FolderTest', '');

Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/Service/NamingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,21 @@ public function getUniqueFilenameDataProvider(): \Generator
'expectation' => 'vfs://root/someSimpleFile.ext'
];

yield 'not existing directory' => [
'filename' => 'vfs://root/directory',
'expectation' => 'vfs://root/directory'
];

yield 'not existing file in directory' => [
'filename' => 'vfs://root/someDirectory/someSimpleFile.ext',
'expectation' => 'vfs://root/someDirectory/someSimpleFile.ext'
];

yield 'not existing directory in directory' => [
'filename' => 'vfs://root/someDirectory/directory',
'expectation' => 'vfs://root/someDirectory/directory'
];

yield 'not existing file in not existing directory' => [
'filename' => 'vfs://root/someOtherDirectory/someSimpleFile.ext',
'expectation' => 'vfs://root/someOtherDirectory/someSimpleFile.ext'
Expand All @@ -93,6 +103,11 @@ public function getUniqueFilenameDataProvider(): \Generator
'expectation' => 'vfs://root/someFile_4.doc'
];

yield 'existing directory' => [
'filename' => 'vfs://root/someDirectory',
'expectation' => 'vfs://root/someDirectory_1'
];

yield 'existing file in directory' => [
'filename' => 'vfs://root/someDirectory/someFilename.txt',
'expectation' => 'vfs://root/someDirectory/someFilename_1.txt'
Expand Down

0 comments on commit 2a26f7f

Please sign in to comment.