Completed
Push — preview-1.19.0 ( c984f9...cb24ce )
by Guilherme
10:55
created

BlockByPhoneCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidPhone() 0 21 4
A getUsers() 0 14 2
A configure() 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\CoreBundle\Command;
12
13
use libphonenumber\NumberParseException;
14
use libphonenumber\PhoneNumber;
15
use LoginCidadao\CoreBundle\Model\PersonInterface;
16
use LoginCidadao\ValidationBundle\Validator\Constraints\MobilePhoneNumberValidator;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Console\Style\SymfonyStyle;
22
23
/**
24
 * Class BlockByPhoneCommand
25
 * @package LoginCidadao\CoreBundle\Command
26
 * @codeCoverageIgnore
27
 */
28
class BlockByPhoneCommand extends AbstractPersonBlockCommand
29
{
30
31
    protected function configure()
32
    {
33
        parent::configure();
34
        $this
35
            ->setName('lc:block-by-phone')
36
            ->addArgument('phone', InputArgument::REQUIRED, 'Mobile number in the E.164 format.')
37
            ->addOption(
38
                'ignore-mobile-validation',
39
                'i',
40
                InputOption::VALUE_NONE,
41
                'Disable the mobile phone validation so you can pass a non-mobile phone')
42
            ->setDescription("Blocks all users that are using the given mobile phone.");
43
    }
44
45
    protected function getUsers(SymfonyStyle $io, InputInterface $input, OutputInterface $output)
46
    {
47
        $phone = $this->getValidPhone($io, $input);
48
        if ($phone instanceof PhoneNumber) {
49
            return [];
50
        }
51
52
        $io->section("Searching users...");
53
        /** @var PersonInterface[] $users */
54
        $users = $this->getEntityManager()
55
            ->getRepository('LoginCidadaoCoreBundle:Person')
56
            ->findBy(['mobile' => $phone, 'enabled' => true]);
57
58
        return $users;
59
    }
60
61
    private function getValidPhone(SymfonyStyle $io, InputInterface $input)
62
    {
63
        $phoneArg = $input->getArgument('phone');
64
        $phoneUtil = $this->getPhoneUtil();
65
        $checkMobile = !$input->getOption('ignore-mobile-validation');
66
67
        try {
68
            $phone = $phoneUtil->parse($phoneArg);
69
70
            if ($checkMobile && false === MobilePhoneNumberValidator::isMobile($phone)) {
71
                $io->error('The given phone is not a mobile phone...');
72
73
                return null;
74
            }
75
        } catch (NumberParseException $e) {
76
            $io->error("'{$phoneArg}' doesn't appear to be a valid phone number.");
77
78
            return null;
79
        }
80
81
        return $phone;
82
    }
83
}
84