Extracting and testing logic to find a folder or make a root folder to the folder repository

parent 354601f4
......@@ -37,7 +37,7 @@ class MoveMediaController extends Controller
public function __invoke(MoveMediaRequest $request)
{
$destination = $this->getDestinationFolder($request->get('destinationFolder'));
$destination = $this->folder->findFolderOrRoot($request->get('destinationFolder'));
$failedMoves = 0;
foreach ($request->get('files') as $file) {
......@@ -50,27 +50,4 @@ class MoveMediaController extends Controller
'folder_id' => $destination->id,
]);
}
private function makeRootFolder() : File
{
return new File([
'id' => 0,
'folder_id' => 0,
'path' => config('asgard.media.config.files-path'),
]);
}
/**
* @param int $destinationFolderId
* @return File
*/
protected function getDestinationFolder($destinationFolderId) : File
{
$destination = $this->folder->findFolder($destinationFolderId);
if ($destination === null) {
$destination = $this->makeRootFolder();
}
return $destination;
}
}
......@@ -107,6 +107,23 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
return $folder;
}
/**
* Find the folder by ID or return a root folder
* which is an instantiated File class
* @param int $folderId
* @return File
*/
public function findFolderOrRoot($folderId): File
{
$destination = $this->findFolder($folderId);
if ($destination === null) {
$destination = $this->makeRootFolder();
}
return $destination;
}
private function getNewPathFor(string $filename, File $folder)
{
return $this->removeDoubleSlashes($folder->path->getRelativeUrl() . '/' . str_slug($filename));
......@@ -132,4 +149,17 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
return config('asgard.media.config.files-path') . str_slug(array_get($data, 'name'));
}
/**
* Create an instantiated File entity, appointed as root
* @return File
*/
private function makeRootFolder() : File
{
return new File([
'id' => 0,
'folder_id' => 0,
'path' => config('asgard.media.config.files-path'),
]);
}
}
......@@ -25,4 +25,12 @@ interface FolderRepository extends BaseRepository
public function allNested() : NestedFoldersCollection;
public function move(File $folder, File $destination) : File;
/**
* Find the folder by ID or return a root folder
* which is an instantiated File class
* @param int $folderId
* @return File
*/
public function findFolderOrRoot($folderId) : File;
}
......@@ -330,6 +330,22 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this->assertCount(2, File::all());
}
/** @test */
public function it_finds_a_folder_or_returns_a_root_folder()
{
$this->folder->create(['name' => 'My Folder']);
$foundFolder = $this->folder->findFolderOrRoot(1);
$this->assertInstanceOf(File::class, $foundFolder);
$this->assertEquals('My Folder', $foundFolder->filename);
$this->assertTrue($foundFolder->exists);
$notFolder = $this->folder->findFolderOrRoot(0);
$this->assertFalse($notFolder->exists);
$this->assertEquals(0, $notFolder->id);
$this->assertEquals(0, $notFolder->folder_id);
$this->assertEquals(config('asgard.media.config.files-path'), $notFolder->path->getRelativeUrl());
}
private function createFile($fileName = 'random/name.jpg')
{
return File::create([
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment