AdminLoanController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 51
c 1
b 0
f 0
dl 0
loc 133
ccs 0
cts 72
cp 0
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteLoan() 0 12 2
A loansList() 0 8 1
A __construct() 0 11 1
B createOrEditILoan() 0 30 6
A getForm() 0 12 3
1
<?php
2
3
namespace App\Controller\Admin;
4
5
use App\Command\DeleteLoanCommand;
6
use App\Repository\LoanRepository;
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\LoanService;
18
use Symfony\Component\Form\FormInterface;
19
20
class AdminLoanController extends AbstractController
21
{
22
    /**
23
     * @var MessageBusInterface
24
     */
25
    private $commandBus;
26
    /**
27
     * @var LoggerInterface
28
     */
29
    private $logger;
30
    /**
31
     * @var LoanRepository
32
     */
33
    private $repository;
34
    /**
35
     * @var LoanService
36
     */
37
    private $loanService;
38
39
    /**
40
     * @param MessageBusInterface $commandBus
41
     * @param LoggerInterface $logger
42
     * @param LoanRepository $repository
43
     * @param LoanService $loanService
44
     */
45
    public function __construct(
46
        MessageBusInterface $commandBus,
47
        LoggerInterface $logger,
48
        LoanRepository $repository,
49
        LoanService $loanService
50
    )
51
    {
52
        $this->commandBus = $commandBus;
53
        $this->logger = $logger;
54
        $this->repository = $repository;
55
        $this->loanService = $loanService;
56
    }
57
58
    /**
59
     * @Route("/admin/loan/{id}", name="admin_loan", defaults={"id"=null}, methods={"GET","POST"})
60
     * 
61
     * @param string|null $id
62
     * @param Request $rawRequest
63
     * @return Response|RedirectResponse
64
     */
65
    public function createOrEditILoan(?string $id, Request $rawRequest): Response
66
    {
67
        $itemId = $rawRequest->query->get('i');
68
69
        if (empty($id) && empty($itemId)) {
70
            $this->addFlash('danger','Please create loan from Items page');
71
            return $this->redirectToRoute('admin_loans');
72
        }
73
74
        $form = $this->getForm($id, $itemId);
75
        $form->handleRequest($rawRequest);
76
77
        try {
78
            if ($form->isSubmitted() && $form->isValid()) {
79
                $loanDTO = $form->getData();
80
                $loanDTO->setId($id);
81
                $command = $this->loanService->getCommand($loanDTO);
82
                $this->commandBus->dispatch($command);
83
                $this->addFlash('success','Your changes were saved!');
84
                return $this->redirectToRoute('admin_loan', ['id' => $command->getId()]);
0 ignored issues
show
Bug introduced by
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

84
                return $this->redirectToRoute('admin_loan', ['id' => $command->/** @scrutinizer ignore-call */ getId()]);
Loading history...
85
            }
86
        } catch (\Exception $e) {
87
            $this->addFlash('danger','Error while saving changes');
88
            $error = new FormError("There is an error: ".$e->getMessage());
89
            $form->addError($error);
90
        }
91
92
        return $this->render('admin/loan/form.html.twig', [
93
            'form' => $form->createView(),
94
            'id' => $id
95
        ]);
96
    }
97
98
    /** 
99
     * @Route("/admin/loans/list", name="admin_loans")
100
     * @param PaginatorInterface $paginator
101
     * @param Request $request
102
     * @return Response 
103
     */ 
104
    public function loansList(PaginatorInterface $paginator, Request $request): Response
105
    {         
106
        $searchQuery = $request->query->getAlnum('search');
107
108
        return $this->render('admin/loan/list.html.twig', [ 
109
            'pagination' => $paginator->paginate(
110
             $this->repository->listAllLoans($searchQuery), $request->query->getInt('page', 1),10),
111
             'searchQuery'  => $searchQuery
112
        ]); 
113
    }
114
115
    /**
116
     * 
117
     * @Route("/admin/delete/loan/{id}", name="admin_delete_loan", methods={"GET"})
118
     * @param string $id
119
     * @param Request $rawRequest
120
     * @return Response|RedirectResponse
121
     */
122
    public function deleteLoan(string $id, Request $rawRequest): Response
0 ignored issues
show
Unused Code introduced by
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

122
    public function deleteLoan(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...
123
    {
124
        try {
125
            $loanId = Uuid::fromString($id);
126
            $command = new DeleteLoanCommand($loanId);
127
            $this->commandBus->dispatch($command);
128
            $this->addFlash('success','Loan deleted!');
129
        } catch (\Exception $e) {
130
            $this->addFlash('danger','Error while deleting loan: '.$e->getMessage());
131
        }
132
133
        return $this->redirectToRoute('admin_loans');
134
    }
135
136
    /**
137
     * @param string|null $id
138
     * @param string|null $itemId
139
     * @return FormInterface
140
     */
141
    private function getForm($id = null, $itemId = null): FormInterface
142
    {
143
        if (!empty($id)) {
144
            $loan = $this->repository->getLoan(Uuid::fromString($id));
145
            $loanDTO = $this->loanService->fillLoanDTO($loan);
146
            return $this->createForm(\App\Form\Type\LoanType::class, $loanDTO);
147
        }
148
149
        if (!empty($itemId)) {
150
            $itemId = Uuid::fromString($itemId);
151
            $loanDTO = $this->loanService->getLoanDTOForItem($itemId);
152
            return $this->createForm(\App\Form\Type\LoanType::class, $loanDTO);
153
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 149 is false. This is incompatible with the type-hinted return Symfony\Component\Form\FormInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
154
    }
155
}
156