Skip to content

Commit c3d14d1

Browse files
committed
Create a new class for getting new names of files plus a test class in preperation for refactoring
1 parent b7c9e07 commit c3d14d1

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\File;
7+
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Exception\FileSystemException;
10+
use Magento\Framework\Validation\ValidationException;
11+
12+
/**
13+
* Class Name
14+
*
15+
* @package Magento\Framework\File
16+
*/
17+
class Name
18+
{
19+
/**
20+
* Get new file name if the given name is in use
21+
*
22+
* @param string $destinationFile
23+
* @return string
24+
*/
25+
public function getNewFileName(string $destinationFile)
26+
{
27+
$fileInfo = $this->getPathInfo($destinationFile);
28+
if ($this->fileExist($destinationFile)) {
29+
$index = 1;
30+
$baseName = $fileInfo['filename'] . '.' . $fileInfo['extension'];
31+
while (file_exists($fileInfo['dirname'] . '/' . $baseName)) {
32+
$baseName = $fileInfo['filename'] . '_' . $index . '.' . $fileInfo['extension'];
33+
$index++;
34+
}
35+
$destFileName = $baseName;
36+
} else {
37+
return $fileInfo['basename'];
38+
}
39+
40+
return $destFileName;
41+
}
42+
43+
/**
44+
* Get the path information from a given file
45+
*
46+
* @param string $destinationFile
47+
* @return string|string[]
48+
*/
49+
private function getPathInfo(string $destinationFile)
50+
{
51+
return pathinfo($destinationFile);
52+
}
53+
54+
/**
55+
* Check to see if a given file exists
56+
*
57+
* @param string $destinationFile
58+
* @return bool
59+
*/
60+
private function fileExist(string $destinationFile)
61+
{
62+
return file_exists($destinationFile);
63+
}
64+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Test\Unit\File;
8+
9+
use Magento\Framework\File\Name;
10+
11+
class NameTest extends \PHPUnit\Framework\TestCase
12+
{
13+
/**
14+
* @var string
15+
*/
16+
private $existingFilePath;
17+
18+
/**
19+
* @var string
20+
*/
21+
private $nonExistingFilePath;
22+
23+
/**
24+
* @var Name
25+
*/
26+
private $name;
27+
28+
protected function setUp()
29+
{
30+
$this->name = new Name();
31+
$this->existingFilePath = __DIR__ . '/../_files/source.txt';
32+
$this->nonExistingFilePath = __DIR__ . '/../_files/file.txt';
33+
}
34+
35+
/**
36+
* @test
37+
*/
38+
public function testGetNewFileNameWhenFileExists()
39+
{
40+
$this->assertEquals('source_1.txt', $this->name->getNewFileName($this->existingFilePath));
41+
}
42+
43+
/**
44+
* @test
45+
*/
46+
public function testGetNewFileNameWhenFileDoesNotExist()
47+
{
48+
$this->assertEquals('file.txt', $this->name->getNewFileName($this->nonExistingFilePath));
49+
}
50+
}

0 commit comments

Comments
 (0)