Failed Conditions
Pull Request — master (#319)
by Guilherme
08:18
created

PersonSupportController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 33 5
A validateSupportTicketId() 0 10 3
A viewAction() 0 17 1
A getPersonGrid() 0 12 1
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\SupportBundle\Controller;
12
13
use Doctrine\ORM\NonUniqueResultException;
14
use Doctrine\ORM\QueryBuilder;
15
use LoginCidadao\APIBundle\Security\Audit\ActionLogger;
16
use LoginCidadao\CoreBundle\Entity\PersonRepository;
17
use LoginCidadao\CoreBundle\Entity\SentEmail;
18
use LoginCidadao\CoreBundle\Helper\GridHelper;
19
use LoginCidadao\CoreBundle\Model\PersonInterface;
20
use LoginCidadao\SupportBundle\Form\PersonSearchFormType;
21
use LoginCidadao\SupportBundle\Model\PersonSearchRequest;
22
use LoginCidadao\SupportBundle\Service\SupportHandler;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
24
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
25
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
26
use Symfony\Component\HttpFoundation\Request;
27
28
/**
29
 * Class PersonSupportController
30
 * @package LoginCidadao\SupportBundle\Controller
31
 *
32
 * @Security("has_role('ROLE_SUPPORT_AGENT')")
33
 * @codeCoverageIgnore
34
 */
35
class PersonSupportController extends Controller
36
{
37
    /**
38
     * @Route("/support/search", name="lc_support_person_search")
39
     */
40
    public function indexAction(Request $request)
41
    {
42
        $gridView = null;
43
        $search = new PersonSearchRequest();
44
45
        $search->smartSearch = $request->get('search', null);
46
47
        $form = $this->createForm(PersonSearchFormType::class, $search);
48
        $form->handleRequest($request);
49
50
        if ($form->isSubmitted() && $form->isValid()) {
51
            /** @var PersonRepository $repo */
52
            $repo = $this->getDoctrine()->getRepository('LoginCidadaoCoreBundle:Person');
53
            $query = $repo->getSmartSearchQuery($search->smartSearch);
54
            try {
55
                $person = $query->getQuery()->getOneOrNullResult();
56
57
                if ($person instanceof PersonInterface) {
58
                    return $this->redirectToRoute('lc_support_person_view', [
59
                        'id' => $person->getId(),
60
                        'ticket' => $search->supportTicket,
61
                    ]);
62
                }
63
            } catch (NonUniqueResultException $e) {
64
                $grid = $this->getPersonGrid($query, $form);
65
                $gridView = $grid->createView($request);
66
            }
67
        }
68
69
        return $this->render('LoginCidadaoSupportBundle:PersonSupport:index.html.twig', [
70
            'form' => $form->createView(),
71
            'grid' => $gridView,
72
            'search' => $search,
73
        ]);
74
    }
75
76
    /**
77
     * @Route("/support/person/{id}", name="lc_support_person_view")
78
     */
79
    public function viewAction(Request $request, $id)
80
    {
81
        $supportRequest = $this->validateSupportTicketId($request->get('ticket'));
82
83
        /** @var SupportHandler $supportHandler */
84
        $supportHandler = $this->get(SupportHandler::class);
85
86
        $person = $supportHandler->getSupportPerson($id);
87
88
        /** @var ActionLogger $actionLogger */
89
        $actionLogger = $this->get('lc.action_logger');
90
        $actionLogger->registerProfileView($request, $person, $this->getUser(), [$this, 'viewAction']);
0 ignored issues
show
Bug introduced by
It seems like $this->getUser() can also be of type null; however, parameter $viewer of LoginCidadao\APIBundle\S...::registerProfileView() does only seem to accept LoginCidadao\CoreBundle\...tifiablePersonInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        $actionLogger->registerProfileView($request, $person, /** @scrutinizer ignore-type */ $this->getUser(), [$this, 'viewAction']);
Loading history...
91
92
        return $this->render('LoginCidadaoSupportBundle:PersonSupport:view.html.twig', [
93
            'person' => $person,
94
            'supportRequest' => $supportRequest,
95
            'dataValidation' => $supportHandler->getValidationMap($person),
96
        ]);
97
    }
98
99
    private function validateSupportTicketId(string $ticket = null): SentEmail
100
    {
101
        /** @var SupportHandler $supportHandler */
102
        $supportHandler = $this->get(SupportHandler::class);
103
        $sentEmail = $ticket ? $supportHandler->getInitialMessage($ticket) : null;
104
        if (!$sentEmail instanceof SentEmail) {
105
            throw $this->createNotFoundException("Invalid Support Ticket ID");
106
        }
107
108
        return $sentEmail;
109
    }
110
111
    private function getPersonGrid(QueryBuilder $query, $form): GridHelper
112
    {
113
        $grid = new GridHelper();
114
        $grid->setId('person-grid');
115
        $grid->setPerPage(5);
116
        $grid->setMaxResult(5);
117
        $grid->setInfiniteGrid(true);
118
        $grid->setRoute('lc_support_person_search');
119
        $grid->setRouteParams([$form->getName()]);
120
        $grid->setQueryBuilder($query);
0 ignored issues
show
Deprecated Code introduced by
The function LoginCidadao\CoreBundle\...lper::setQueryBuilder() has been deprecated: since version 1.1.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

120
        /** @scrutinizer ignore-deprecated */ $grid->setQueryBuilder($query);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
121
122
        return $grid;
123
    }
124
}
125