Creating logic to update a folder in db

parent a8295577
<?php
namespace Modules\Media\Events;
use Modules\Core\Contracts\EntityIsChanging;
use Modules\Core\Events\AbstractEntityHook;
class FolderIsUpdating extends AbstractEntityHook implements EntityIsChanging
{
}
<?php
namespace Modules\Media\Events;
use Modules\Media\Entities\File;
class FolderWasUpdated
{
/**
* @var File
*/
public $folder;
/**
* @var array
*/
public $data;
public function __construct(File $folder, array $data)
{
$this->folder = $folder;
$this->data = $data;
}
}
......@@ -5,7 +5,9 @@ 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\FolderIsUpdating;
use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Events\FolderWasUpdated;
use Modules\Media\Repositories\FolderRepository;
class EloquentFolderRepository extends EloquentBaseRepository implements FolderRepository
......@@ -36,6 +38,20 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
return $folder;
}
public function update($model, $data)
{
$formattedData = [
'filename' => array_get($data, 'name'),
'path' => $this->getPath($data),
];
event($event = new FolderIsUpdating($formattedData));
$model->update($event->getAttributes());
event(new FolderWasUpdated($model, $formattedData));
return $model;
}
/**
* @param array $data
* @return string
......
......@@ -5,7 +5,9 @@ namespace Modules\Media\Tests;
use Illuminate\Support\Facades\Event;
use Modules\Media\Entities\File;
use Modules\Media\Events\FolderIsCreating;
use Modules\Media\Events\FolderIsUpdating;
use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Events\FolderWasUpdated;
use Modules\Media\Repositories\FolderRepository;
final class EloquentFolderRepositoryTest extends MediaTestCase
......@@ -110,4 +112,56 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$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')));
}
/** @test */
public function it_can_update_a_folder_in_database()
{
$folder = $this->folder->create(['name' => 'My Folder', 'parent_id' => 0]);
$folder = $this->folder->update($folder, ['name' => 'New Name!']);
$this->assertCount(1, $this->folder->all());
$this->assertEquals('New Name!', $folder->filename);
$this->assertEquals('/assets/media/new-name', $folder->path->getRelativeUrl());
}
/** @test */
public function it_triggers_event_when_folder_was_updated()
{
Event::fake();
$folder = $this->folder->create(['name' => 'My Folder', 'parent_id' => 0]);
$folder = $this->folder->update($folder, ['name' => 'New Name!']);
Event::assertDispatched(FolderWasUpdated::class, function ($e) use ($folder) {
return $e->folder->id === $folder->id;
});
}
/** @test */
public function it_triggers_an_event_when_folder_is_updating()
{
Event::fake();
$folder = $this->folder->create(['name' => 'My Folder']);
$folder = $this->folder->update($folder, ['name' => 'New Name!']);
Event::assertDispatched(FolderIsUpdating::class, function ($e) use ($folder) {
return $e->getAttribute('filename') === $folder->filename;
});
}
/** @test */
public function it_can_change_data_when_folder_is_updating()
{
Event::listen(FolderIsUpdating::class, function (FolderIsUpdating $event) {
$filename = $event->getAttribute('filename');
$event->setAttributes(['filename' => strtoupper($filename)]);
});
$folder = $this->folder->create(['name' => 'My Folder']);
$folder = $this->folder->update($folder, ['name' => 'New Name!']);
$this->assertEquals('NEW NAME!', $folder->filename);
}
}
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