Skip to content

Commit

Permalink
Extract global moving logic to a Mover class
Browse files Browse the repository at this point in the history
  • Loading branch information
nWidart committed Oct 10, 2017
1 parent bc7a09b commit 354601f
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 24 deletions.
30 changes: 6 additions & 24 deletions Modules/Media/Http/Controllers/Api/MoveMediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use Modules\Media\Http\Requests\MoveMediaRequest;
use Modules\Media\Repositories\FileRepository;
use Modules\Media\Repositories\FolderRepository;
use Modules\Media\Services\Movers\FileMover;
use Modules\Media\Services\Movers\FolderMover;
use Modules\Media\Services\Movers\Mover;

class MoveMediaController extends Controller
{
Expand All @@ -21,25 +20,19 @@ class MoveMediaController extends Controller
*/
private $folder;
/**
* @var FolderMover
* @var Mover
*/
private $folderMover;
/**
* @var FileMover
*/
private $fileMover;
private $mover;

public function __construct(
FileRepository $file,
FolderRepository $folder,
FolderMover $folderMover,
FileMover $fileMover
Mover $mover
)
{
$this->file = $file;
$this->folder = $folder;
$this->folderMover = $folderMover;
$this->fileMover = $fileMover;
$this->mover = $mover;
}

public function __invoke(MoveMediaRequest $request)
Expand All @@ -48,18 +41,7 @@ public function __invoke(MoveMediaRequest $request)

$failedMoves = 0;
foreach ($request->get('files') as $file) {
$file = $this->file->find($file['id']);
// $this->>mover->move($file, $destination)
if ($file->is_folder === false) {
if ($this->fileMover->move($file, $destination) === false) {
$failedMoves++;
}
}
if ($file->is_folder === true) {
if ($this->folderMover->move($file, $destination) === false) {
$failedMoves++;
}
}
$failedMoves = $this->mover->move($this->file->find($file['id']), $destination);
}

return response()->json([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function createFromFile(UploadedFile $file, int $parentId = 0)
'mimetype' => $file->getClientMimeType(),
'filesize' => $file->getFileInfo()->getSize(),
'folder_id' => $parentId,
'is_folder' => 0,
];

event($event = new FileIsCreating($data));
Expand Down
37 changes: 37 additions & 0 deletions Modules/Media/Services/Movers/Mover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Modules\Media\Services\Movers;

use Modules\Media\Entities\File;

class Mover
{
/**
* @var FileMover
*/
private $fileMover;
/**
* @var FolderMover
*/
private $folderMover;

public function __construct(FileMover $fileMover, FolderMover $folderMover)
{
$this->fileMover = $fileMover;
$this->folderMover = $folderMover;
}

public function move(File $file, File $destination) : int
{
$failedMoves = 0;

if ($file->is_folder === false && $this->fileMover->move($file, $destination) === false) {
$failedMoves++;
}
if ($file->is_folder === true && $this->folderMover->move($file, $destination) === false) {
$failedMoves++;
}

return $failedMoves;
}
}
82 changes: 82 additions & 0 deletions Modules/Media/Tests/MoverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Modules\Media\Tests;

use Modules\Media\Repositories\FolderRepository;
use Modules\Media\Services\FileService;
use Modules\Media\Services\Movers\Mover;

final class MoverTest extends MediaTestCase
{
/**
* @var Mover
*/
private $mover;

public function setUp()
{
parent::setUp();

$this->resetDatabase();

$this->mover = app(Mover::class);
//$this->file = app(FileRepository::class);
$this->app['config']->set('asgard.media.config.files-path', '/assets/media/');
}

public function tearDown()
{
if ($this->app['files']->isDirectory(public_path('assets')) === true) {
$this->app['files']->deleteDirectory(public_path('assets'));
}
}

/** @test */
public function it_detects_its_a_file_to_move()
{
$folderRepository = app(FolderRepository::class);
$parentFolder = $folderRepository->create(['name' => 'My Folder', 'parent_id' => 0]);

$file = app(FileService::class)->store(\Illuminate\Http\UploadedFile::fake()->create('my-file.pdf'));

$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-file.pdf')));
$failedAmount = $this->mover->move($file, $parentFolder);
$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/my-file.pdf')));
$this->assertSame(0, $failedAmount);
}

/** @test */
public function it_detects_its_a_folder_to_move()
{
$folderRepository = app(FolderRepository::class);
$parentFolder = $folderRepository->create(['name' => 'My Folder', 'parent_id' => 0]);
$childFolder = $folderRepository->create(['name' => 'Child Folder', 'parent_id' => 0]);

$this->assertTrue($this->app['files']->isDirectory(public_path('/assets/media/child-folder')));
$failedAmount = $this->mover->move($childFolder, $parentFolder);
$this->assertTrue($this->app['files']->isDirectory(public_path('/assets/media/my-folder/child-folder')));
$this->assertSame(0, $failedAmount);
}

/** @test */
public function it_counts_amount_of_failed_moves()
{
$folderRepository = app(FolderRepository::class);
$folder = $folderRepository->create(['name' => 'My Folder', 'parent_id' => 0]);
$file = app(FileService::class)->store(\Illuminate\Http\UploadedFile::fake()->image('my-file.jpg'), $folder->id);
$fileTwo = app(FileService::class)->store(\Illuminate\Http\UploadedFile::fake()->image('my-file.jpg'));

$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-file.jpg')));
$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/my-file.jpg')));
$this->assertEquals('/assets/media/my-folder/my-file.jpg', $file->path->getRelativeUrl());
$this->assertEquals('/assets/media/my-file.jpg', $fileTwo->path->getRelativeUrl());

$failedAmount = $this->mover->move($fileTwo, $folder);

$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-file.jpg')));
$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/my-file.jpg')));
$this->assertEquals('/assets/media/my-folder/my-file.jpg', $file->path->getRelativeUrl());
$this->assertEquals('/assets/media/my-file.jpg', $fileTwo->path->getRelativeUrl());
$this->assertSame(1, $failedAmount);
}
}

0 comments on commit 354601f

Please sign in to comment.