Okay... I've done a bit of refactoring...
In my controller I now have:
/**
* @param CategoryRequest $request
*
* @return RedirectResponse
*/
public function store(CategoryRequest $request)
{
$this->dispatch(
new CreateCategory(
$request->get('name'),
$request->get('description'),
$request->file('thumbnail')
)
);
return redirect(route('admin.category.index'));
}
And my command looks like this:
<?php namespace App\Commands;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class CreateCategory extends Command {
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $description;
/**
* @var UploadedFile
*/
private $thumbnail;
/**
* @return void
*/
public function __construct($name, $description, UploadedFile $thumbnail)
{
$this->name = $name;
$this->description = $description;
$this->thumbnail = $thumbnail;
}
/**
* @return string
*/
public function name()
{
return $this->name;
}
/**
* @return null|string
*/
public function description()
{
return empty($this->description) ? null : $this->description;
}
/**
* @return UploadedFile
*/
public function thumbnail()
{
return $this->thumbnail;
}
}
And my handler looks like this:
<?php namespace App\Handlers\Commands;
use App\Commands\CreateCategory;
use App\Model\Category;
class CreateCategoryHandler {
/**
* @param CreateCategory $command
*
* @return void
*/
public function handle(CreateCategory $command)
{
$filename = time() . '-' . str_slug($command->name()) . '.jpg';
$command->thumbnail()->move(public_path() . '/uploads/images/categories', $filename);
Category::create(
[
'name' => $command->name(),
'description' => $command->description(),
'thumbnail' => $filename
]
);
}
}
Is this better? What about using the Filesystem component in the handler?