REFACTORING #1880 @40m: move creating new directory into interaction
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace App\CMS\Interactions\Admin\FileManager;
|
||||
|
||||
use App\CMS\Contracts\Interaction;
|
||||
use App\CMS\Exceptions\FileManager\SubdirectoryExistsInDirectory;
|
||||
use App\CMS\Repositories\FileManager\Directory as DirectoryRepository;
|
||||
use App\Models\FileManager\Directory;
|
||||
|
||||
class CreateDirectory implements Interaction
|
||||
{
|
||||
/**
|
||||
* @var DirectoryRepository
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
public function __construct(DirectoryRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform given action
|
||||
*
|
||||
* @param $data array input data
|
||||
* @param $parameters array parameters
|
||||
*
|
||||
* @return Directory
|
||||
* @throws SubdirectoryExistsInDirectory
|
||||
*/
|
||||
public function handle($data, $parameters)
|
||||
{
|
||||
$data['parentDirectory'] = isset($data['parentDirectory']) ? $data['parentDirectory'] : null;
|
||||
|
||||
// todo this should probably go into validation, but we are not able to get $data in getValidationRules
|
||||
// check if created folder does not exist in current directory
|
||||
if ($this->repository->checkIfNameExists($data['name'], $data['parentDirectory'])) {
|
||||
throw new SubdirectoryExistsInDirectory();
|
||||
}
|
||||
|
||||
// todo use Directory::create() and rename columns/fields in request
|
||||
// create new database entry
|
||||
$directory = new Directory();
|
||||
$directory->name = $data['name'];
|
||||
$directory->parent_id = $data['parentDirectory'];
|
||||
$directory->user_id = \Auth::user()->id;
|
||||
$directory->description = $data['description'];
|
||||
$directory->save();
|
||||
|
||||
return $directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data array input data
|
||||
*
|
||||
* @return array
|
||||
* @throws SubdirectoryExistsInDirectory
|
||||
*/
|
||||
public function getValidationRules($data = [])
|
||||
{
|
||||
return [
|
||||
'name' => 'required|max:255',
|
||||
'parentDirectory' => 'exists:directories,id'
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user