Skip to content

Commit

Permalink
Extracting and testing logic to find a folder or make a root folder t…
Browse files Browse the repository at this point in the history
…o the folder repository
  • Loading branch information
nWidart committed Oct 10, 2017
1 parent 354601f commit ad5d2e3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
25 changes: 1 addition & 24 deletions Modules/Media/Http/Controllers/Api/MoveMediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
30 changes: 30 additions & 0 deletions Modules/Media/Repositories/Eloquent/EloquentFolderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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'),
]);
}
}
8 changes: 8 additions & 0 deletions Modules/Media/Repositories/FolderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
16 changes: 16 additions & 0 deletions Modules/Media/Tests/EloquentFolderRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit ad5d2e3

Please sign in to comment.