Failed Conditions
Push — issue#699 ( 7155bc...11c53a )
by Guilherme
04:07
created

PersonSupportController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 84
ccs 0
cts 60
cp 0
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 13 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\CoreBundle\Entity\PersonRepository;
16
use LoginCidadao\CoreBundle\Entity\SentEmail;
17
use LoginCidadao\CoreBundle\Helper\GridHelper;
18
use LoginCidadao\CoreBundle\Model\PersonInterface;
19
use LoginCidadao\SupportBundle\Form\PersonSearchFormType;
20
use LoginCidadao\SupportBundle\Model\PersonSearchRequest;
21
use LoginCidadao\SupportBundle\Service\SupportHandler;
22
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
24
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
25
use Symfony\Component\HttpFoundation\Request;
26
27
/**
28
 * Class PersonSupportController
29
 * @package LoginCidadao\SupportBundle\Controller
30
 *
31
 * @Security("has_role('ROLE_SUPPORT_AGENT')")
32
 */
33
class PersonSupportController extends Controller
34
{
35
    /**
36
     * @Route("/support/search", name="lc_support_person_search")
37
     */
38
    public function indexAction(Request $request)
39
    {
40
        $gridView = null;
41
        $search = new PersonSearchRequest();
42
43
        $search->smartSearch = $request->get('search', null);
44
45
        $form = $this->createForm(PersonSearchFormType::class, $search);
46
        $form->handleRequest($request);
47
48
        if ($form->isSubmitted() && $form->isValid()) {
49
            /** @var PersonRepository $repo */
50
            $repo = $this->getDoctrine()->getRepository('LoginCidadaoCoreBundle:Person');
51
            $query = $repo->getSmartSearchQuery($search->smartSearch);
52
            try {
53
                $person = $query->getQuery()->getOneOrNullResult();
54
55
                if ($person instanceof PersonInterface) {
56
                    return $this->redirectToRoute('lc_support_person_view', [
57
                        'id' => $person->getId(),
58
                        'ticket' => $search->supportTicket,
59
                    ]);
60
                }
61
            } catch (NonUniqueResultException $e) {
62
                $grid = $this->getPersonGrid($query, $form);
63
                $gridView = $grid->createView($request);
64
            }
65
        }
66
67
        return $this->render('LoginCidadaoSupportBundle:PersonSupport:index.html.twig', [
68
            'form' => $form->createView(),
69
            'grid' => $gridView,
70
            'search' => $search,
71
        ]);
72
    }
73
74
    /**
75
     * @Route("/support/person/{id}", name="lc_support_person_view")
76
     */
77
    public function viewAction(Request $request, $id)
78
    {
79
        $supportRequest = $this->validateSupportTicketId($request->get('ticket'));
80
81
        /** @var SupportHandler $supportHandler */
82
        $supportHandler = $this->get(SupportHandler::class);
83
84
        $person = $supportHandler->getSupportPerson($id);
85
86
        return $this->render('LoginCidadaoSupportBundle:PersonSupport:view.html.twig', [
87
            'person' => $person,
88
            'supportRequest' => $supportRequest,
89
            'dataValidation' => $supportHandler->getValidationMap($person),
90
        ]);
91
    }
92
93
    private function validateSupportTicketId(string $ticket = null): SentEmail
94
    {
95
        /** @var SupportHandler $supportHandler */
96
        $supportHandler = $this->get(SupportHandler::class);
97
        $sentEmail = $ticket ? $supportHandler->getInitialMessage($ticket) : null;
98
        if (!$sentEmail instanceof SentEmail) {
99
            throw $this->createNotFoundException("Invalid Support Ticket ID");
100
        }
101
102
        return $sentEmail;
103
    }
104
105
    private function getPersonGrid(QueryBuilder $query, $form): GridHelper
106
    {
107
        $grid = new GridHelper();
108
        $grid->setId('person-grid');
109
        $grid->setPerPage(5);
110
        $grid->setMaxResult(5);
111
        $grid->setInfiniteGrid(true);
112
        $grid->setRoute('lc_support_person_search');
113
        $grid->setRouteParams([$form->getName()]);
114
        $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

114
        /** @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...
115
116
        return $grid;
117
    }
118
}
119