Skip to content
Snippets Groups Projects
DeleteGroupAction.php 1.30 KiB
<?php
declare(strict_types=1);

namespace App\Application\Actions\Group;
use App\Domain\Group\GroupNotFoundException;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Domain\Group\Group ;

class DeleteGroupAction extends GroupAction
{
    /**
     * {@inheritdoc}
     */
    protected function action(): Response
    {
        if(!isset($_SESSION['userId'])){
            $this->flash->addMessage('flashMessages', 'Please log in or sign up.');

            return $this->response
            ->withHeader('Location', '/login')
            ->withStatus(302);
        }

        $groupId = (int) $this->resolveArg('id');
        $group = $this->groupRepository->find($groupId);

        if (!isset($group)) {
            throw new GroupNotFoundException();
        }

        if (!$group->checkAdmin($_SESSION['userId'])){
            return $this->response
            ->withHeader('Location', '/account')
            ->withStatus(302);
        }

        $this->em->remove($group);
        $this->em->flush();

        $this->logger->info("Group has been deleted.");
        $this->flash->addMessage('delete', 'The group has been deleted.');

        return $this->response
            ->withHeader('Location', '/groups')
            ->withStatus(302);
    }

}