1
0
mirror of https://github.com/flarum/core.git synced 2025-07-15 22:06:24 +02:00
Files
php-flarum/src/Core/Command/DeleteAvatarHandler.php
2017-10-03 18:47:23 +02:00

82 lines
1.9 KiB
PHP

<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Core\Command;
use Flarum\Core\Access\AssertPermissionTrait;
use Flarum\User\Exception\PermissionDeniedException;
use Flarum\User\UserRepository;
use Flarum\Core\Support\DispatchEventsTrait;
use Flarum\Event\AvatarWillBeDeleted;
use Illuminate\Contracts\Events\Dispatcher;
use League\Flysystem\FilesystemInterface;
class DeleteAvatarHandler
{
use DispatchEventsTrait;
use AssertPermissionTrait;
/**
* @var UserRepository
*/
protected $users;
/**
* @var FilesystemInterface
*/
protected $uploadDir;
/**
* @param Dispatcher $events
* @param UserRepository $users
* @param FilesystemInterface $uploadDir
*/
public function __construct(Dispatcher $events, UserRepository $users, FilesystemInterface $uploadDir)
{
$this->events = $events;
$this->users = $users;
$this->uploadDir = $uploadDir;
}
/**
* @param DeleteAvatar $command
* @return \Flarum\User\User
* @throws \Flarum\User\Exception\PermissionDeniedException
*/
public function handle(DeleteAvatar $command)
{
$actor = $command->actor;
$user = $this->users->findOrFail($command->userId);
if ($actor->id !== $user->id) {
$this->assertCan($actor, 'edit', $user);
}
$avatarPath = $user->avatar_path;
$user->changeAvatarPath(null);
$this->events->fire(
new AvatarWillBeDeleted($user, $actor)
);
$user->save();
if ($this->uploadDir->has($avatarPath)) {
$this->uploadDir->delete($avatarPath);
}
$this->dispatchEventsFor($user, $actor);
return $user;
}
}