Completed
Push — issue#841 ( 22c862 )
by Guilherme
07:41
created

PersonSupportController::getDummySupportMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

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

131
        /** @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...
132
133
        return $grid;
134
    }
135
136
    private function getDummySupportMessage()
137
    {
138
        return (new SentEmail())
139
            ->setType('dummy')
140
            ->setDate(new \DateTime())
141
            ->setSubject('Dummy message')
142
            ->setMessage('Dummy message')
143
            ->setSender('Dummy');
144
    }
145
}
146