From ad5d2e36be74b32896ea49c133d4e8cfaaedc766 Mon Sep 17 00:00:00 2001 From: Nicolas Widart Date: Tue, 10 Oct 2017 16:00:05 +0200 Subject: [PATCH] Extracting and testing logic to find a folder or make a root folder to the folder repository --- .../Controllers/Api/MoveMediaController.php | 25 +--------------- .../Eloquent/EloquentFolderRepository.php | 30 +++++++++++++++++++ .../Media/Repositories/FolderRepository.php | 8 +++++ .../Tests/EloquentFolderRepositoryTest.php | 16 ++++++++++ 4 files changed, 55 insertions(+), 24 deletions(-) diff --git a/Modules/Media/Http/Controllers/Api/MoveMediaController.php b/Modules/Media/Http/Controllers/Api/MoveMediaController.php index 2946ce0ce..65feae154 100644 --- a/Modules/Media/Http/Controllers/Api/MoveMediaController.php +++ b/Modules/Media/Http/Controllers/Api/MoveMediaController.php @@ -37,7 +37,7 @@ public function __construct( 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 @@ public function __invoke(MoveMediaRequest $request) '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; - } } diff --git a/Modules/Media/Repositories/Eloquent/EloquentFolderRepository.php b/Modules/Media/Repositories/Eloquent/EloquentFolderRepository.php index a5d021748..b277c5d78 100644 --- a/Modules/Media/Repositories/Eloquent/EloquentFolderRepository.php +++ b/Modules/Media/Repositories/Eloquent/EloquentFolderRepository.php @@ -107,6 +107,23 @@ public function move(File $folder, File $destination): File 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 @@ private function getPath(array $data): string 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'), + ]); + } } diff --git a/Modules/Media/Repositories/FolderRepository.php b/Modules/Media/Repositories/FolderRepository.php index cc73a9d6c..767fe88a0 100644 --- a/Modules/Media/Repositories/FolderRepository.php +++ b/Modules/Media/Repositories/FolderRepository.php @@ -25,4 +25,12 @@ public function allChildrenOf(File $folder); 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; } diff --git a/Modules/Media/Tests/EloquentFolderRepositoryTest.php b/Modules/Media/Tests/EloquentFolderRepositoryTest.php index 8a8e3e549..c72b829b5 100644 --- a/Modules/Media/Tests/EloquentFolderRepositoryTest.php +++ b/Modules/Media/Tests/EloquentFolderRepositoryTest.php @@ -330,6 +330,22 @@ public function it_removes_folder_and_files_from_database() $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([