johnykvsky /
spisywarka
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace App\Controller\Admin; |
||||
| 4 | |||||
| 5 | use App\Command\DeleteCollectionCommand; |
||||
| 6 | use App\Repository\CollectionRepository; |
||||
| 7 | use Psr\Log\LoggerInterface; |
||||
| 8 | use Ramsey\Uuid\Uuid; |
||||
| 9 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
||||
| 10 | use Symfony\Component\HttpFoundation\JsonResponse; |
||||
| 11 | use Symfony\Component\HttpFoundation\Response; |
||||
| 12 | use Symfony\Component\HttpFoundation\Request; |
||||
| 13 | use Symfony\Component\Messenger\MessageBusInterface; |
||||
| 14 | use Symfony\Component\Routing\Annotation\Route; |
||||
| 15 | use Symfony\Component\Form\FormError; |
||||
| 16 | use Knp\Component\Pager\PaginatorInterface; |
||||
| 17 | use Symfony\Component\HttpFoundation\RedirectResponse; |
||||
| 18 | use App\Service\CollectionService; |
||||
| 19 | use Symfony\Component\Form\FormInterface; |
||||
| 20 | use Swagger\Annotations as SWG; |
||||
| 21 | use App\Traits\RequestQueryTrait; |
||||
| 22 | |||||
| 23 | class AdminCollectionController extends AbstractController |
||||
| 24 | { |
||||
| 25 | use RequestQueryTrait; |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 26 | |||||
| 27 | /** |
||||
| 28 | * @var MessageBusInterface |
||||
| 29 | */ |
||||
| 30 | private $commandBus; |
||||
| 31 | /** |
||||
| 32 | * @var LoggerInterface |
||||
| 33 | */ |
||||
| 34 | private $logger; |
||||
| 35 | /** |
||||
| 36 | * @var CollectionRepository |
||||
| 37 | */ |
||||
| 38 | private $repository; |
||||
| 39 | /** |
||||
| 40 | * @var CollectionService |
||||
| 41 | */ |
||||
| 42 | private $collectionService; |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * @param MessageBusInterface $commandBus |
||||
| 46 | * @param LoggerInterface $logger |
||||
| 47 | * @param CollectionRepository $repository |
||||
| 48 | * @param CollectionService $collectionService |
||||
| 49 | */ |
||||
| 50 | public function __construct( |
||||
| 51 | MessageBusInterface $commandBus, |
||||
| 52 | LoggerInterface $logger, |
||||
| 53 | CollectionRepository $repository, |
||||
| 54 | CollectionService $collectionService |
||||
| 55 | ) |
||||
| 56 | { |
||||
| 57 | $this->commandBus = $commandBus; |
||||
| 58 | $this->logger = $logger; |
||||
| 59 | $this->repository = $repository; |
||||
| 60 | $this->collectionService = $collectionService; |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | /** |
||||
| 64 | * |
||||
| 65 | * @Route("/admin/collection/{id}", name="admin_collection", defaults={"id"=null}, methods={"GET","POST"}) |
||||
| 66 | * @param string|null $id |
||||
| 67 | * @param Request $rawRequest |
||||
| 68 | * @return Response|RedirectResponse |
||||
| 69 | */ |
||||
| 70 | public function createOrEditCollection(?string $id, Request $rawRequest): Response |
||||
| 71 | { |
||||
| 72 | $form = $this->getForm($id); |
||||
| 73 | $form->handleRequest($rawRequest); |
||||
| 74 | |||||
| 75 | try { |
||||
| 76 | if ($form->isSubmitted() && $form->isValid()) { |
||||
| 77 | $collectionDTO = $form->getData(); |
||||
| 78 | $collectionDTO->setId($id); |
||||
| 79 | $command = $this->collectionService->getCommand($collectionDTO); |
||||
| 80 | $this->commandBus->dispatch($command); |
||||
| 81 | $this->addFlash('success','Your changes were saved!'); |
||||
| 82 | return $this->redirectToRoute('admin_collection', ['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...
|
|||||
| 83 | } |
||||
| 84 | } catch (\Exception $e) { |
||||
| 85 | $this->addFlash('danger','Error while saving changes'); |
||||
| 86 | $error = new FormError("There is an error: ".$e->getMessage()); |
||||
| 87 | $form->addError($error); |
||||
| 88 | } |
||||
| 89 | |||||
| 90 | return $this->render('admin/collection/form.html.twig', [ |
||||
| 91 | 'form' => $form->createView(), |
||||
| 92 | 'id' => $id |
||||
| 93 | ]); |
||||
| 94 | } |
||||
| 95 | |||||
| 96 | /** |
||||
| 97 | * @Route("/admin/collections/list", name="admin_collections") |
||||
| 98 | * @param PaginatorInterface $paginator |
||||
| 99 | * @param Request $request |
||||
| 100 | * @return Response |
||||
| 101 | */ |
||||
| 102 | public function collectionList(PaginatorInterface $paginator, Request $request): Response |
||||
| 103 | { |
||||
| 104 | return $this->render('admin/collection/list.html.twig', [ |
||||
| 105 | 'pagination' => $paginator->paginate( |
||||
| 106 | $this->repository->listAllCollections(), $request->query->getInt('page', 1),10) |
||||
| 107 | ]); |
||||
| 108 | } |
||||
| 109 | |||||
| 110 | /** |
||||
| 111 | * |
||||
| 112 | * @Route("/admin/delete/collection/{id}", name="admin_delete_collection", methods={"GET"}) |
||||
| 113 | * @param string $id |
||||
| 114 | * @param Request $rawRequest |
||||
| 115 | * @return Response|RedirectResponse |
||||
| 116 | */ |
||||
| 117 | public function deleteCollection(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...
|
|||||
| 118 | { |
||||
| 119 | try { |
||||
| 120 | $collectionId = Uuid::fromString($id); |
||||
| 121 | $command = new DeleteCollectionCommand($collectionId); |
||||
| 122 | $this->commandBus->dispatch($command); |
||||
| 123 | $this->addFlash('success','Collection deleted!'); |
||||
| 124 | } catch (\Exception $e) { |
||||
| 125 | $this->addFlash('danger','Error while deleting collection: '.$e->getMessage()); |
||||
| 126 | } |
||||
| 127 | |||||
| 128 | return $this->redirectToRoute('admin_collections'); |
||||
| 129 | } |
||||
| 130 | |||||
| 131 | /** |
||||
| 132 | * @Route("/autocomplete/collections", name="admin_collections_autocomplete", methods={"GET"}) |
||||
| 133 | * |
||||
| 134 | * @SWG\Tag(name="Autocomplete") |
||||
| 135 | * @SWG\Get( |
||||
| 136 | * @SWG\Parameter(name="q", in="path", type="string", description="Search query") |
||||
| 137 | * ) |
||||
| 138 | * @SWG\Response( |
||||
| 139 | * response="200", |
||||
| 140 | * description="Matched collections" |
||||
| 141 | * ) |
||||
| 142 | * |
||||
| 143 | * @param Request $request |
||||
| 144 | * @return JsonResponse |
||||
| 145 | */ |
||||
| 146 | public function autocompleteAction(Request $request): JsonResponse |
||||
| 147 | { |
||||
| 148 | $result = array(); |
||||
| 149 | $q = $request->query->get('q'); |
||||
| 150 | $searchQuery = filter_var(urldecode($q), FILTER_SANITIZE_STRING); |
||||
|
0 ignored issues
–
show
|
|||||
| 151 | |||||
| 152 | $collections = $this->repository->autocomplete($this->getFromRequest($request, 'q', true)); |
||||
| 153 | |||||
| 154 | foreach ($collections as $collection) { |
||||
| 155 | $result[] = ['id' => $collection->getId()->toString(), 'text'=>$collection->getName()]; |
||||
| 156 | } |
||||
| 157 | |||||
| 158 | return $this->json(['collections' => $result], 200); |
||||
| 159 | } |
||||
| 160 | |||||
| 161 | /** |
||||
| 162 | * @param string|null $id |
||||
| 163 | * @return FormInterface |
||||
| 164 | */ |
||||
| 165 | private function getForm($id = null): FormInterface |
||||
| 166 | { |
||||
| 167 | if (empty($id)) { |
||||
| 168 | return $this->createForm(\App\Form\Type\CollectionType::class); |
||||
| 169 | } |
||||
| 170 | |||||
| 171 | $collection = $this->repository->getCollection(Uuid::fromString($id)); |
||||
| 172 | $collectionDTO = $this->collectionService->fillCollectionDTO($collection); |
||||
| 173 | return $this->createForm(\App\Form\Type\CollectionType::class, $collectionDTO); |
||||
| 174 | } |
||||
| 175 | } |
||||
| 176 |