Passed
Pull Request — master (#317)
by Guilherme
03:39
created

AdminBlocklistController::listAction()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 3
nop 1
dl 0
loc 29
rs 9.6333
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\PhoneVerificationBundle\Controller;
12
13
use LoginCidadao\CoreBundle\Helper\GridHelper;
14
use LoginCidadao\PhoneVerificationBundle\Entity\BlockedPhoneNumber;
15
use LoginCidadao\PhoneVerificationBundle\Entity\BlockedPhoneNumberRepository;
16
use LoginCidadao\PhoneVerificationBundle\Form\BlockPhoneFormType;
17
use LoginCidadao\PhoneVerificationBundle\Form\SearchPhoneNumberType;
18
use LoginCidadao\PhoneVerificationBundle\Model\BlockPhoneNumberRequest;
19
use LoginCidadao\PhoneVerificationBundle\Service\Blocklist;
20
use LoginCidadao\PhoneVerificationBundle\Service\BlocklistInterface;
21
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Session\Session;
24
use Symfony\Component\Routing\Annotation\Route;
25
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
27
28
/**
29
 * Class AdminBlocklistController
30
 * @package LoginCidadao\PhoneVerificationBundle\Controller
31
 *
32
 * @Security("has_role('ROLE_EDIT_BLOCKED_PHONES')")
33
 * @codeCoverageIgnore
34
 */
35
class AdminBlocklistController extends Controller
36
{
37
    /**
38
     * @Route("/admin/phones/blocklist", name="phone_blocklist_list")
39
     * @Template()
40
     */
41
    public function listAction(Request $request)
42
    {
43
        $form = $this->createForm(SearchPhoneNumberType::class);
44
45
        $form->handleRequest($request);
46
        if ($form->isSubmitted() && $form->isValid()) {
47
            $data = $form->getData();
48
49
            $grid = new GridHelper();
50
            $grid->setId('person-grid');
51
            $grid->setPerPage(5);
52
            $grid->setMaxResult(5);
53
            $grid->setInfiniteGrid(true);
54
            $grid->setRoute('lc_admin_person_grid');
55
            $grid->setRouteParams([$form->getName()]);
56
57
            if ($data['phone']) {
58
                /** @var BlockedPhoneNumberRepository $blockedPhoneRepo */
59
                $blockedPhoneRepo = $this->getDoctrine()->getRepository(BlockedPhoneNumber::class);
60
                $query = $blockedPhoneRepo->getSearchByPartialPhoneQuery($data['phone']);
61
                $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

61
                /** @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...
62
            }
63
64
            $gridView = $grid->createView($request);
65
        }
66
67
        return [
68
            'form' => $form->createView(),
69
            'grid' => $gridView ?? null,
70
        ];
71
    }
72
73
    /**
74
     * @Route("/admin/phones/blocklist/new", name="phone_blocklist_new")
75
     * @Template()
76
     */
77
    public function newAction(Request $request)
78
    {
79
        $blockRequest = new BlockPhoneNumberRequest($this->getUser());
0 ignored issues
show
Bug introduced by
It seems like $this->getUser() can also be of type null; however, parameter $blockedBy of LoginCidadao\PhoneVerifi...rRequest::__construct() does only seem to accept LoginCidadao\CoreBundle\Model\PersonInterface, 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

79
        $blockRequest = new BlockPhoneNumberRequest(/** @scrutinizer ignore-type */ $this->getUser());
Loading history...
80
81
        $blockedPhones = [];
82
        $form = $this->createForm(BlockPhoneFormType::class, $blockRequest);
83
84
        $form->handleRequest($request);
85
        if ($form->isSubmitted() && $form->isValid()) {
86
            $phoneNumber = $blockRequest->phoneNumber;
87
88
            $blocklist = $this->getBlocklistService();
89
            $blockedPhoneNumber = $blocklist->addBlockedPhoneNumber($phoneNumber, $blockRequest->getBlockedBy());
90
            $blockedPhones = $blocklist->checkPhoneNumber($blockedPhoneNumber->getPhoneNumber());
91
92
            /** @var Session $session */
93
            $session = $request->getSession();
94
            $session->getFlashBag()->add('success', "Phone number successfully banned.");
95
            if (($count = count($blockedPhones)) > 0) {
96
                $session->getFlashBag()->add('success', "{$count} accounts were blocked blocked.");
97
            }
98
99
            return $this->redirectToRoute('phone_blocklist_list');
100
        }
101
102
        return [
103
            'form' => $form->createView(),
104
            'blockedPhones' => $blockedPhones,
105
        ];
106
    }
107
108
    /**
109
     * @return BlocklistInterface
110
     */
111
    private function getBlocklistService(): BlocklistInterface
112
    {
113
        /** @var BlocklistInterface $blocklistService */
114
        $blocklistService = $this->get('phone_verification.blocklist');
115
116
        return $blocklistService;
117
    }
118
}
119