johnykvsky /
spisywarka
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace App\Controller\Admin; |
||||
| 4 | |||||
| 5 | use App\Command\DeleteUserCommand; |
||||
| 6 | use App\Repository\UserRepository; |
||||
| 7 | use Psr\Log\LoggerInterface; |
||||
| 8 | use Ramsey\Uuid\Uuid; |
||||
| 9 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
||||
| 10 | use Symfony\Component\HttpFoundation\Response; |
||||
| 11 | use Symfony\Component\HttpFoundation\Request; |
||||
| 12 | use Symfony\Component\Messenger\MessageBusInterface; |
||||
| 13 | use Symfony\Component\Routing\Annotation\Route; |
||||
| 14 | use Symfony\Component\Form\FormError; |
||||
| 15 | use Knp\Component\Pager\PaginatorInterface; |
||||
| 16 | use Symfony\Component\HttpFoundation\RedirectResponse; |
||||
| 17 | use App\Service\UserService; |
||||
| 18 | use Symfony\Component\Form\FormInterface; |
||||
| 19 | use App\Traits\RequestQueryTrait; |
||||
| 20 | |||||
| 21 | class AdminUserController extends AbstractController |
||||
| 22 | { |
||||
| 23 | use RequestQueryTrait; |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 24 | |||||
| 25 | /** |
||||
| 26 | * @var MessageBusInterface |
||||
| 27 | */ |
||||
| 28 | private $commandBus; |
||||
| 29 | /** |
||||
| 30 | * @var LoggerInterface |
||||
| 31 | */ |
||||
| 32 | private $logger; |
||||
| 33 | /** |
||||
| 34 | * @var UserRepository |
||||
| 35 | */ |
||||
| 36 | private $repository; |
||||
| 37 | /** |
||||
| 38 | * @var UserService |
||||
| 39 | */ |
||||
| 40 | |||||
| 41 | private $userService; |
||||
| 42 | /** |
||||
| 43 | * @param MessageBusInterface $commandBus |
||||
| 44 | * @param LoggerInterface $logger |
||||
| 45 | * @param UserRepository $repository |
||||
| 46 | * @param UserService $userService |
||||
| 47 | */ |
||||
| 48 | public function __construct( |
||||
| 49 | MessageBusInterface $commandBus, |
||||
| 50 | LoggerInterface $logger, |
||||
| 51 | UserRepository $repository, |
||||
| 52 | UserService $userService |
||||
| 53 | ) |
||||
| 54 | { |
||||
| 55 | $this->commandBus = $commandBus; |
||||
| 56 | $this->logger = $logger; |
||||
| 57 | $this->repository = $repository; |
||||
| 58 | $this->userService = $userService; |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * @Route("/admin/user/{id}", name="admin_user", defaults={"id"=null}, methods={"GET","POST"}) |
||||
| 63 | * |
||||
| 64 | * @param string|null $id |
||||
| 65 | * @param Request $rawRequest |
||||
| 66 | * @return Response|RedirectResponse |
||||
| 67 | */ |
||||
| 68 | public function createOrEditUser(?string $id, Request $rawRequest): Response |
||||
| 69 | { |
||||
| 70 | $form = $this->getForm($id); |
||||
| 71 | $form->handleRequest($rawRequest); |
||||
| 72 | |||||
| 73 | try { |
||||
| 74 | if ($form->isSubmitted() && $form->isValid()) { |
||||
| 75 | $userDTO = $form->getData(); |
||||
| 76 | $userDTO->setId($id); |
||||
| 77 | $command = $this->userService->getCommand($userDTO); |
||||
| 78 | $this->commandBus->dispatch($command); |
||||
| 79 | $this->addFlash('success','Your changes were saved!'); |
||||
| 80 | return $this->redirectToRoute('admin_user', ['id' => $command->getId()]); |
||||
|
0 ignored issues
–
show
The method
getId() does not exist on App\Command\CommandInterface. It seems like you code against a sub-type of said class. However, the method does not exist in App\Command\ResetPasswordCommand or App\Command\ResetPasswordConfirmationCommand or App\Command\AddItemToCollectionCommand or App\Command\RemoveItemFromCollectionCommand. Are you sure you never get one of those?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 81 | } |
||||
| 82 | } catch (\Exception $e) { |
||||
| 83 | $this->addFlash('danger','Error while saving changes'); |
||||
| 84 | $error = new FormError("There is an error: ".$e->getMessage()); |
||||
| 85 | $form->addError($error); |
||||
| 86 | } |
||||
| 87 | |||||
| 88 | return $this->render('admin/user/form.html.twig', [ |
||||
| 89 | 'form' => $form->createView(), |
||||
| 90 | 'id' => $id |
||||
| 91 | ]); |
||||
| 92 | } |
||||
| 93 | |||||
| 94 | /** |
||||
| 95 | * @Route("/admin/users/list", name="admin_users") |
||||
| 96 | * @param PaginatorInterface $paginator |
||||
| 97 | * @param Request $request |
||||
| 98 | * @return Response |
||||
| 99 | */ |
||||
| 100 | public function usersList(PaginatorInterface $paginator, Request $request): Response |
||||
| 101 | { |
||||
| 102 | $searchQuery = $request->query->getAlnum('search'); |
||||
| 103 | |||||
| 104 | return $this->render('admin/user/list.html.twig', [ |
||||
| 105 | 'pagination' => $paginator->paginate( |
||||
| 106 | $this->repository->listAllUsers($searchQuery), $request->query->getInt('page', 1),10), |
||||
| 107 | 'searchQuery' => $searchQuery |
||||
| 108 | ]); |
||||
| 109 | } |
||||
| 110 | |||||
| 111 | /** |
||||
| 112 | * |
||||
| 113 | * @Route("/admin/delete/user/{id}", name="admin_delete_user", methods={"GET"}) |
||||
| 114 | * @param string $id |
||||
| 115 | * @param Request $rawRequest |
||||
| 116 | * @return Response|RedirectResponse |
||||
| 117 | */ |
||||
| 118 | public function deleteUser(string $id, Request $rawRequest): Response |
||||
|
0 ignored issues
–
show
The parameter
$rawRequest is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 119 | { |
||||
| 120 | try { |
||||
| 121 | $userId = Uuid::fromString($id); |
||||
| 122 | $command = new DeleteUserCommand($userId); |
||||
| 123 | $this->commandBus->dispatch($command); |
||||
| 124 | $this->addFlash('success','User deleted!'); |
||||
| 125 | } catch (\Exception $e) { |
||||
| 126 | $this->addFlash('danger','Error while deleting user: '.$e->getMessage()); |
||||
| 127 | } |
||||
| 128 | |||||
| 129 | return $this->redirectToRoute('admin_users'); |
||||
| 130 | } |
||||
| 131 | |||||
| 132 | /** |
||||
| 133 | * @param string|null $id |
||||
| 134 | * @return FormInterface |
||||
| 135 | */ |
||||
| 136 | private function getForm($id = null): FormInterface |
||||
| 137 | { |
||||
| 138 | if (empty($id)) { |
||||
| 139 | return $this->createForm(\App\Form\Type\UserType::class); |
||||
| 140 | } |
||||
| 141 | |||||
| 142 | $user = $this->repository->getUser(Uuid::fromString($id)); |
||||
| 143 | $userDTO = $this->userService->fillUserDTO($user); |
||||
| 144 | return $this->createForm(\App\Form\Type\UserType::class, $userDTO); |
||||
| 145 | } |
||||
| 146 | } |
||||
| 147 |