Issues (62)

src/Controller/Admin/AdminItemController.php (4 issues)

1
<?php
2
3
namespace App\Controller\Admin;
4
5
use App\Command\DeleteItemCommand;
6
use App\Repository\ItemRepository;
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\ItemService;
19
use Symfony\Component\Form\FormInterface;
20
use Swagger\Annotations as SWG;
21
use App\Traits\RequestQueryTrait;
22
23
class AdminItemController extends AbstractController
24
{
25
    use RequestQueryTrait;
0 ignored issues
show
The trait App\Traits\RequestQueryTrait requires the property $query which is not provided by App\Controller\Admin\AdminItemController.
Loading history...
26
27
    /**
28
     * @var MessageBusInterface
29
     */
30
    private $commandBus;
31
    /**
32
     * @var LoggerInterface
33
     */
34
    private $logger;
35
    /**
36
     * @var ItemRepository
37
     */
38
    private $repository;
39
    /**
40
     * @var ItemService
41
     */
42
    private $itemService;
43
44
    /**
45
     * @param MessageBusInterface $commandBus
46
     * @param LoggerInterface $logger
47
     * @param ItemRepository $repository
48
     * @param ItemService $itemService
49
     */
50
    public function __construct(
51
        MessageBusInterface $commandBus,
52
        LoggerInterface $logger,
53
        ItemRepository $repository,
54
        ItemService $itemService
55
    )
56
    {
57
        $this->commandBus = $commandBus;
58
        $this->logger = $logger;
59
        $this->repository = $repository;
60
        $this->itemService = $itemService;
61
    }
62
63
    /**
64
     * @Route("/admin/item/{id}", name="admin_item", defaults={"id"=null}, methods={"GET","POST"})
65
     * 
66
     * @param string|null $id
67
     * @param Request $rawRequest
68
     * @return Response|RedirectResponse
69
     */
70
    public function createOrEditItem(?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
                $itemDTO = $form->getData();
78
                $itemDTO->setId($id);
79
                $command = $this->itemService->getCommand($itemDTO);
80
                $this->commandBus->dispatch($command);
81
                $this->addFlash('success','Your changes were saved!');
82
                return $this->redirectToRoute('admin_item', ['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 ignore-call  annotation

82
                return $this->redirectToRoute('admin_item', ['id' => $command->/** @scrutinizer ignore-call */ getId()]);
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/item/form.html.twig', [
91
            'form' => $form->createView(),
92
            'id' => $id
93
        ]);
94
    }
95
96
    /** 
97
     * @Route("/admin/items/list", name="admin_items") 
98
     * @param PaginatorInterface $paginator
99
     * @param Request $request
100
     * @return Response
101
     */ 
102
    public function itemsList(PaginatorInterface $paginator, Request $request): Response 
103
    {         
104
        $searchQuery = $request->query->getAlnum('search');
105
106
        return $this->render('admin/item/list.html.twig', [ 
107
            'pagination' => $paginator->paginate(
108
             $this->repository->listAllItems($searchQuery), $request->query->getInt('page', 1),10),
109
             'searchQuery'  => $searchQuery
110
        ]); 
111
    }
112
113
    /**
114
     * 
115
     * @Route("/admin/delete/item/{id}", name="admin_delete_item", methods={"GET"})
116
     * @param string $id
117
     * @param Request $rawRequest
118
     * @return Response|RedirectResponse
119
     */
120
    public function deleteItem(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 ignore-unused  annotation

120
    public function deleteItem(string $id, /** @scrutinizer ignore-unused */ Request $rawRequest): Response

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...
121
    {
122
        try {
123
            $itemId = Uuid::fromString($id);
124
            $command = new DeleteItemCommand($itemId);
125
            $this->commandBus->dispatch($command);
126
            $this->addFlash('success','Item deleted!');
127
        } catch (\Exception $e) {
128
            $this->addFlash('danger','Error while deleting item: '.$e->getMessage());
129
        }
130
131
        return $this->redirectToRoute('admin_items');
132
    }
133
134
    /** 
135
     * @Route("/autocomplete/items", name="admin_items_autocomplete", methods={"GET"}) 
136
     *
137
     * @SWG\Tag(name="Autocomplete")
138
     * @SWG\Get(
139
     *     @SWG\Parameter(name="q", in="path", type="string", description="Search query")
140
     * )
141
     * @SWG\Response(
142
     *     response="200",
143
     *     description="Matched items"
144
     * )
145
     *
146
     * @param Request $request
147
     * @return JsonResponse
148
     */ 
149
    public function autocompleteAction(Request $request): JsonResponse
150
    {
151
        $result = array();
152
        $q = $request->query->get('q');
153
        $searchQuery = filter_var(urldecode($q), FILTER_SANITIZE_STRING);
0 ignored issues
show
The assignment to $searchQuery is dead and can be removed.
Loading history...
154
155
        $items = $this->repository->autocomplete($this->getFromRequest($request, 'q', true));
156
157
        foreach ($items as $item) {
158
            $result[] = ['id' => $item->getId()->toString(), 'text'=>$item->getName()];
159
        }
160
161
        return $this->json(['items' => $result], 200);
162
    }
163
164
    /**
165
     * @param string|null $id
166
     * @return FormInterface
167
     */
168
    private function getForm($id = null): FormInterface
169
    {
170
        if (empty($id)) {
171
            return $this->createForm(\App\Form\Type\ItemType::class);
172
        }
173
174
        $item = $this->repository->getItem(Uuid::fromString($id));
175
        $itemDTO = $this->itemService->fillItemDTO($item);
176
        return $this->createForm(\App\Form\Type\ItemType::class, $itemDTO);
177
    }
178
}
179