Completed
Push — preview-1.22.1 ( f5103a )
by
unknown
04:56 queued 10s
created

PersonSupportController::viewAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 3
nop 2
dl 0
loc 28
rs 9.6666
c 0
b 0
f 0
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
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29
30
/**
31
 * Class PersonSupportController
32
 * @package LoginCidadao\SupportBundle\Controller
33
 *
34
 * @Security("has_role('ROLE_SUPPORT_AGENT')")
35
 * @codeCoverageIgnore
36
 */
37
class PersonSupportController extends Controller
38
{
39
    /**
40
     * @Route("/support/search", name="lc_support_person_search")
41
     * @Security("has_role('ROLE_SUPPORT_SEARCH_USERS')")
42
     * @param Request $request
43
     * @return Response
44
     */
45
    public function searchAction(Request $request)
46
    {
47
        $gridView = null;
48
        $search = new PersonSearchRequest();
49
50
        $search->smartSearch = $request->get('search', null);
51
52
        $form = $this->createForm(PersonSearchFormType::class, $search);
53
        $form->handleRequest($request);
54
55
        if ($form->isSubmitted() && $form->isValid()) {
56
            /** @var PersonRepository $repo */
57
            $repo = $this->getDoctrine()->getRepository('LoginCidadaoCoreBundle:Person');
58
            $query = $repo->getSmartSearchQuery($search->smartSearch);
59
            try {
60
                $person = $query->getQuery()->getOneOrNullResult();
61
62
                if ($person instanceof PersonInterface) {
63
                    return $this->redirectToRoute('lc_support_person_view', [
64
                        'id' => $person->getId(),
65
                        'ticket' => $search->supportTicket,
66
                    ]);
67
                }
68
            } catch (NonUniqueResultException $e) {
69
                $grid = $this->getPersonGrid($query, $form);
70
                $gridView = $grid->createView($request);
71
            }
72
        }
73
74
        return $this->render('LoginCidadaoSupportBundle:PersonSupport:index.html.twig', [
75
            'form' => $form->createView(),
76
            'grid' => $gridView,
77
            'search' => $search,
78
        ]);
79
    }
80
81
    /**
82
     * @Route("/support/person/{id}", name="lc_support_person_view")
83
     * @param Request $request
84
     * @param $id
85
     * @return Response
86
     */
87
    public function viewAction(Request $request, $id)
88
    {
89
        try {
90
            $supportRequest = $this->validateSupportTicketId($request->get('ticket'));
91
        } catch (NotFoundHttpException $e) {
92
            if (!$this->isGranted('ROLE_SKIP_SUPPORT_TOKEN_VALIDATION')) {
93
                throw $e;
94
            }
95
            $supportRequest = null;
96
        }
97
98
        /** @var SupportHandler $supportHandler */
99
        $supportHandler = $this->get(SupportHandler::class);
100
101
        $person = $supportHandler->getSupportPerson($id);
102
        $phoneMetadata = $supportHandler->getPhoneMetadata($person);
103
        $thirdPartyConnections = $supportHandler->getThirdPartyConnections($person);
104
105
        /** @var ActionLogger $actionLogger */
106
        $actionLogger = $this->get('lc.action_logger');
107
        $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

107
        $actionLogger->registerProfileView($request, $person, /** @scrutinizer ignore-type */ $this->getUser(), [$this, 'viewAction']);
Loading history...
108
109
        return $this->render('LoginCidadaoSupportBundle:PersonSupport:view.html.twig', [
110
            'person' => $person,
111
            'thirdPartyConnections' => $thirdPartyConnections,
112
            'supportRequest' => $supportRequest,
113
            'dataValidation' => $supportHandler->getValidationMap($person),
114
            'phoneMetadata' => $phoneMetadata,
115
        ]);
116
    }
117
118
    private function validateSupportTicketId(string $ticket = null): SentEmail
119
    {
120
        /** @var SupportHandler $supportHandler */
121
        $supportHandler = $this->get(SupportHandler::class);
122
        $sentEmail = $ticket ? $supportHandler->getInitialMessage($ticket) : null;
123
        if (!$sentEmail instanceof SentEmail) {
124
            throw $this->createNotFoundException("Invalid Support Ticket ID");
125
        }
126
127
        return $sentEmail;
128
    }
129
130
    private function getPersonGrid(QueryBuilder $query, $form): GridHelper
131
    {
132
        $grid = new GridHelper();
133
        $grid->setId('person-grid');
134
        $grid->setPerPage(5);
135
        $grid->setMaxResult(5);
136
        $grid->setInfiniteGrid(true);
137
        $grid->setRoute('lc_support_person_search');
138
        $grid->setRouteParams([$form->getName()]);
139
        $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

139
        /** @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...
140
141
        return $grid;
142
    }
143
}
144