Adding tests & logic to remove folders and its children

parent 90b92a9c
<?php
namespace Modules\Media\Events\Handlers;
use Modules\Media\Events\FolderIsDeleting;
use Modules\Media\Repositories\FolderRepository;
class DeleteAllChildrenOfFolder
{
/**
* @var FolderRepository
*/
private $folder;
public function __construct(FolderRepository $folder)
{
$this->folder = $folder;
}
public function handle(FolderIsDeleting $event)
{
$children = $this->folder->allChildrenOf($event->folder);
foreach ($children as $child) {
$child->delete();
}
}
}
<?php
namespace Modules\Media\Events\Handlers;
use Illuminate\Contracts\Filesystem\Factory;
use Modules\Media\Events\FolderIsDeleting;
class DeleteFolderOnDisk
{
/**
* @var Factory
*/
private $finder;
public function __construct(Factory $finder)
{
$this->finder = $finder;
}
public function handle(FolderIsDeleting $event)
{
$this->finder->disk($this->getConfiguredFilesystem())->deleteDirectory($this->getDestinationPath($event->folder->getOriginal('path')));
}
/**
* @param string $path
* @return string
*/
private function getDestinationPath($path)
{
if ($this->getConfiguredFilesystem() === 'local') {
return basename(public_path()) . $path;
}
return $path;
}
/**
* @return string
*/
private function getConfiguredFilesystem()
{
return config('asgard.media.config.filesystem');
}
}
......@@ -2,6 +2,7 @@
namespace Modules\Media\Repositories\Eloquent;
use Illuminate\Database\Eloquent\Collection;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Media\Entities\File;
use Modules\Media\Events\FolderIsCreating;
......@@ -71,6 +72,17 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
return $folder->delete();
}
/**
* @param File $folder
* @return Collection
*/
public function allChildrenOf(File $folder)
{
$path = $folder->path->getRelativeUrl();
return $this->model->where('path', 'like', "{$path}%")->get();
}
/**
* @param array $data
* @return string
......
......@@ -2,6 +2,7 @@
namespace Modules\Media\Repositories;
use Illuminate\Database\Eloquent\Collection;
use Modules\Core\Repositories\BaseRepository;
use Modules\Media\Entities\File;
......@@ -13,4 +14,10 @@ interface FolderRepository extends BaseRepository
* @return File|null
*/
public function findFolder(int $folderId);
/**
* @param File $folder
* @return Collection
*/
public function allChildrenOf(File $folder);
}
......@@ -262,6 +262,60 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
});
}
/** @test */
public function it_deletes_folder_from_disk()
{
$folder = $this->folder->create(['name' => 'My Folder']);
$this->assertTrue($this->app['files']->isDirectory(public_path('assets/media/my-folder')));
$this->folder->destroy($folder);
$this->assertFalse($this->app['files']->isDirectory(public_path('assets/media/my-folder')));
}
/** @test */
public function it_deletes_folder_in_subfolder_on_disk()
{
$parentFolder = $this->folder->create(['name' => 'My Folder']);
$folder = $this->folder->create(['name' => 'Subfolder', 'parent_id' => $parentFolder->id]);
$this->assertTrue($this->app['files']->isDirectory(public_path('assets/media/my-folder/subfolder')));
$this->folder->destroy($folder);
$this->assertFalse($this->app['files']->isDirectory(public_path('assets/media/my-folder/subfolder')));
}
/** @test */
public function it_can_remove_folder_with_files()
{
$parentFolder = $this->folder->create(['name' => 'My Folder']);
$folderTwo = $this->folder->create(['name' => 'Child folder', 'parent_id' => $parentFolder->id]);
$file = \Illuminate\Http\UploadedFile::fake()->image('my-file.jpg');
$fileTwo = \Illuminate\Http\UploadedFile::fake()->image('my-second-file.jpg');
$fileThree = \Illuminate\Http\UploadedFile::fake()->image('my-third-file.jpg');
app(FileService::class)->store($file, $parentFolder->id);
app(FileService::class)->store($fileTwo, $folderTwo->id);
app(FileService::class)->store($fileThree, $folderTwo->id);
$this->folder->destroy($folderTwo);
$this->assertFalse($this->app['files']->isDirectory(public_path('assets/media/my-folder/child-folder')));
}
/** @test */
public function it_removes_folder_and_files_from_database()
{
$parentFolder = $this->folder->create(['name' => 'My Folder']);
$folderTwo = $this->folder->create(['name' => 'Child folder', 'parent_id' => $parentFolder->id]);
$file = \Illuminate\Http\UploadedFile::fake()->image('my-file.jpg');
$fileTwo = \Illuminate\Http\UploadedFile::fake()->image('my-second-file.jpg');
$fileThree = \Illuminate\Http\UploadedFile::fake()->image('my-third-file.jpg');
app(FileService::class)->store($file, $parentFolder->id);
app(FileService::class)->store($fileTwo, $folderTwo->id);
app(FileService::class)->store($fileThree, $folderTwo->id);
$this->assertCount(5, File::all());
$this->folder->destroy($folderTwo);
$this->assertCount(2, File::all());
}
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