diff --git a/src/Application/Actions/Group/DeleteUserGroupAction.php b/src/Application/Actions/Group/DeleteUserGroupAction.php new file mode 100644 index 0000000000000000000000000000000000000000..f269753ac11ac39d65e45b5adaae1b4f88e5cae1 --- /dev/null +++ b/src/Application/Actions/Group/DeleteUserGroupAction.php @@ -0,0 +1,47 @@ +<?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 DeleteUserGroupAction extends GroupAction +{ + /** + * {@inheritdoc} + */ + protected function action(): Response + { + $groupId = (int) $this->resolveArg('id'); + $group = $this->groupRepository->find($groupId); + + $user_id = (int) $this->resolveArg('user_id'); + + if (!isset($group)) { + throw new GroupNotFoundException(); + } + + if (!$group->checkAdmin($user_id)) + { + return $this->response + ->withHeader('Location', '/groups/' . $groupId) + ->withStatus(302); + } + + $user = $this->userRepository->find($user_id); + $user->removeGroup($group); + + $this->em->persist($group); + $this->em->flush(); + + $this->logger->info("User has been deleted in group."); + $this->flash->addMessage('delete_user', 'User have been deleted in group.'); + + return $this->response + ->withHeader('Location', '/groups/' . $groupId) + ->withStatus(302); + } + +}