MigrateCommand::convert()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @link https://github.com/vuongxuongminh/migrate-phone-number
4
 * @copyright Copyright (c) 2018 Vuong Xuong Minh
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace VXM\MPN;
9
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Helper\ProgressBar;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Question\ConfirmationQuestion;
15
16
/**
17
 * Lớp trừu tượng MigrateCommand hổ trợ các phương thức cơ bản cho việc chuyển đổi số điện thoại 11 số sang 10 số, giúp cho các lớp
18
 * kế thừa thực thi đơn giản hơn.
19
 *
20
 * @author Vuong Minh <[email protected]>
21
 * @since 1.0
22
 */
23
abstract class MigrateCommand extends Command
24
{
25
26
    /**
27
     * Chuỗi đường dẫn url hướng dẫn sử dụng.
28
     */
29
    const HELP_URL = 'https://github.com/vuongxuongminh/migrate-phone-number';
30
31
    /**
32
     * Chuổi hằng regex pattern dùng để định vị số điện thoại 11 số trên db.
33
     */
34
    const MIGRATE_PATTERN = '~(^|\'|")(\+?84|0)?(16[2-9]|12[0-9]|18[68]|199)(\d{7})($|\'|")~';
35
36
    /**
37
     * Mảng hằng dùng để chuyển đổi đầu số.
38
     */
39
    const MIGRATE_MAP = [
40
        '162' => '32', '163' => '33', '164' => '34', '165' => '35', '166' => '36', '167' => '37',
41
        '168' => '38', '169' => '39', '120' => '70', '121' => '79', '122' => '77', '123' => '83',
42
        '124' => '84', '125' => '85', '126' => '76', '127' => '81', '128' => '78', '129' => '82',
43
        '186' => '56', '188' => '58', '199' => '59'
44
    ];
45
46
    /**
47
     * @var null|InputInterface Đối tượng Input khi thực thi lệnh. Nó chỉ có giá trị khi phương thực [[execute()]] được gọi.
48
     */
49
    protected $inputted;
50
51
    /**
52
     * @var null|OutputInterface Đối tượng Output khi thực thi lệnh. Nó chỉ có giá trị khi phương thực [[execute()]] được gọi.
53
     */
54
    protected $outputted;
55
56
    /**
57
     * @inheritdoc
58
     */
59 3
    protected function configure(): void
60
    {
61 3
        $this->setHelp(static::HELP_URL);
62
63 3
        parent::configure();
64 3
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 3
    protected function execute(InputInterface $input, OutputInterface $output): void
70
    {
71 3
        $this->inputted = $input;
72 3
        $this->outputted = $output;
73
74
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $question */
75 3
        $question = $this->getHelper('question');
76 3
        $confirm = new ConfirmationQuestion('<comment>Lệnh sẽ thực hiện thay đổi dữ liệu của bạn, hãy cân nhắc sao lưu dữ liệu trước khi thực hiện lệnh. Bạn có muốn tiếp tục? (y/n): </comment>', false);
77
78 3
        if ($question->ask($input, $output, $confirm)) {
79 3
            $this->migrate();
80
        }
81 3
    }
82
83
    /**
84
     * Phương thức thức trừu tượng đảm nhiệm việc thực thi chuyển đổi số điện thoại 11 số sang 10.
85
     */
86
    abstract protected function migrate(): void;
87
88
    /**
89
     * Phương thức hổ trợ chuyển đổi số điện thoại 11 số sang 10 số.
90
     *
91
     * @param string $phoneNumber Số điện thoại 11 số cần chuyển đổi
92
     * @return string Số điện thoại sau khi chuyển đổi
93
     */
94
    final protected function convert(string $phoneNumber): string
95
    {
96 1
        return preg_replace_callback(self::MIGRATE_PATTERN, function ($matches) {
97 1
            $matches[3] = self::MIGRATE_MAP[$matches[3]];
98 1
            array_shift($matches);
99
100 1
            return implode('', $matches);
101 1
        }, $phoneNumber);
102
    }
103
104
}
105