Failed Conditions
Push — issue#765 ( 54a2c2...25bda7 )
by Guilherme
09:33
created

BlockByPhoneCommand::getUsers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 3
dl 0
loc 14
ccs 0
cts 11
cp 0
crap 6
rs 9.4285
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\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
class BlockByPhoneCommand extends AbstractPersonBlockCommand
24
{
25
26
    protected function configure()
27
    {
28
        parent::configure();
29
        $this
30
            ->setName('lc:block-by-phone')
31
            ->addArgument('phone', InputArgument::REQUIRED, 'Mobile number in the E.164 format.')
32
            ->addOption(
33
                'ignore-mobile-validation',
34
                'i',
35
                InputOption::VALUE_NONE,
36
                'Disable the mobile phone validation so you can pass a non-mobile phone')
37
            ->setDescription("Blocks all users that are using the given mobile phone.");
38
    }
39
40
    protected function getUsers(SymfonyStyle $io, InputInterface $input, OutputInterface $output)
41
    {
42
        $phone = $this->getValidPhone($io, $input);
43
        if ($phone instanceof PhoneNumber) {
44
            return [];
45
        }
46
47
        $io->section("Searching users...");
48
        /** @var PersonInterface[] $users */
49
        $users = $this->getEntityManager()
50
            ->getRepository('LoginCidadaoCoreBundle:Person')
51
            ->findBy(['mobile' => $phone, 'enabled' => true]);
52
53
        return $users;
54
    }
55
56
    private function getValidPhone(SymfonyStyle $io, InputInterface $input)
57
    {
58
        $phoneArg = $input->getArgument('phone');
59
        $phoneUtil = $this->getPhoneUtil();
60
        $checkMobile = !$input->getOption('ignore-mobile-validation');
61
62
        try {
63
            $phone = $phoneUtil->parse($phoneArg);
64
65
            if ($checkMobile && false === MobilePhoneNumberValidator::isMobile($phone)) {
66
                $io->error('The given phone is not a mobile phone...');
67
68
                return null;
69
            }
70
        } catch (NumberParseException $e) {
71
            $io->error("'{$phoneArg}' doesn't appear to be a valid phone number.");
72
73
            return null;
74
        }
75
76
        return $phone;
77
    }
78
}
79