Adding method to find a folder.

Adding logic to set the correct folder path for children
parent a3992a41
......@@ -3,17 +3,28 @@
namespace Modules\Media\Repositories\Eloquent;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Media\Entities\File;
use Modules\Media\Events\FolderIsCreating;
use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Repositories\FolderRepository;
class EloquentFolderRepository extends EloquentBaseRepository implements FolderRepository
{
/**
* Find a folder by its ID
* @param int $folderId
* @return File
*/
public function findFolder(int $folderId): File
{
return $this->model->where('is_folder', 1)->where('id', $folderId)->first();
}
public function create($data)
{
$data = [
'filename' => array_get($data, 'name'),
'path' => config('asgard.media.config.files-path') . str_slug(array_get($data, 'name')),
'path' => $this->getPath($data),
'is_folder' => true,
'folder_id' => array_get($data, 'parent_id'),
];
......@@ -24,4 +35,19 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
return $folder;
}
/**
* @param array $data
* @return string
*/
private function getPath(array $data): string
{
if (array_key_exists('parent_id', $data)) {
$parent = $this->findFolder($data['parent_id']);
return $parent->path->getRelativeUrl() . '/' . str_slug(array_get($data, 'name'));
}
return config('asgard.media.config.files-path') . str_slug(array_get($data, 'name'));
}
}
......@@ -3,7 +3,14 @@
namespace Modules\Media\Repositories;
use Modules\Core\Repositories\BaseRepository;
use Modules\Media\Entities\File;
interface FolderRepository extends BaseRepository
{
/**
* Find a folder by its ID
* @param int $folderId
* @return File
*/
public function findFolder(int $folderId) : File;
}
......@@ -3,6 +3,7 @@
namespace Modules\Media\Tests;
use Illuminate\Support\Facades\Event;
use Modules\Media\Entities\File;
use Modules\Media\Events\FolderIsCreating;
use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Repositories\FolderRepository;
......@@ -89,6 +90,27 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this->assertTrue($this->app['files']->isDirectory(public_path('assets/media/my-folder')));
}
/** @test */
public function it_can_find_a_folder()
{
$this->folder->create(['name' => 'My Folder']);
$folder = $this->folder->findFolder(1);
$this->assertInstanceOf(File::class, $folder);
$this->assertEquals(1, $folder->id);
}
/** @test */
public function it_can_create_folders_belonging_to_another_folder()
{
$this->folder->create(['name' => 'Root Folder']);
$childFolder = $this->folder->create(['name' => 'Child folder', 'parent_id' => 1]);
$this->assertEquals('/assets/media/root-folder/child-folder', $childFolder->path->getRelativeUrl());
$this->assertTrue($this->app['files']->isDirectory(public_path('assets/media/root-folder/child-folder')));
}
private function resetDatabase()
{
// Makes sure the migrations table is created
......
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