CheckExistingAddresses   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
eloc 48
c 3
b 0
f 0
dl 0
loc 95
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B execute() 0 56 7
A configure() 0 5 1
1
<?php
2
3
namespace Hryvinskyi\QuoteAddressValidator\Console\Command;
4
5
use Hryvinskyi\QuoteAddressValidator\Model\AddressValidationInterface;
6
use Magento\Quote\Model\ResourceModel\Quote\Address\CollectionFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Model\Reso...dress\CollectionFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class CheckExistingAddresses extends Command
13
{
14
    /**
15
     * @var CollectionFactory 
16
     */
17
    private $quoteAddressCollectionFactory;
18
19
    /**
20
     * @var AddressValidationInterface 
21
     */
22
    private $addressValidation;
23
    
24
    public function __construct(
25
        CollectionFactory $quoteAddressCollectionFactory,
26
        AddressValidationInterface $addressValidation
27
    ) {
28
        $this->quoteAddressCollectionFactory = $quoteAddressCollectionFactory;
29
        $this->addressValidation = $addressValidation;
30
        parent::__construct();
31
    }
32
33
    /**
34
     * Initialization of the command.
35
     */
36
    protected function configure()
37
    {
38
        $this->setName('hryvinskyi:quote-address-validator:check-existing-addresses');
39
        $this->setDescription('Check existing addresses in the database');
40
        parent::configure();
41
    }
42
43
    /**
44
     * CLI command description.
45
     *
46
     * @param InputInterface $input
47
     * @param OutputInterface $output
48
     *
49
     * @return void
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        $formatter = $this->getHelper('formatter');
54
        $infoStyle = new OutputFormatterStyle('white', 'blue');
55
        $output->getFormatter()->setStyle('info', $infoStyle);
56
        $formattedInfoBlock = $formatter->formatBlock(['INFO:', 'Check existing addresses in the database'], 'info', TRUE);
57
58
        $output->writeln('');
59
        $output->writeln($formattedInfoBlock);
60
        $output->writeln('');
61
62
63
        $successStyle = new OutputFormatterStyle('white', 'green');
64
        $output->getFormatter()->setStyle('success', $successStyle);
65
66
        // Get all addresses
67
        $collection = $this->quoteAddressCollectionFactory->create();
68
        $collection->addFieldToSelect('*');
69
        $collection->setPageSize(50);
70
        $pages = $collection->getLastPageNumber();
71
        $foundAddresses = [];
72
        $found = 0;
73
74
        for ($pageNum = 1; $pageNum <= $pages; $pageNum++) {
75
            $collection->setCurPage($pageNum);
76
            foreach ($collection as $item) {
77
                foreach ($this->addressValidation->getValidations() as $validation) {
78
                    try {
79
                        $validation->execute($item);
80
                    } catch (\Exception $e) {
81
                        $foundAddresses[$item->getId()] = 1;
82
                        $found++;
83
                        $addressType = $item->getAddressType() ? '<success>' . $item->getAddressType() . '</success>' : '<error>EMPTY</error>';
84
                        $output->writeln('Address type: ' . $addressType . '</info>; ID: <success>' . $item->getId() . '</success>; Message: <fg=#c0392b;bg=black>' . $e->getMessage() . '</>');
85
                    }
86
                }
87
            }
88
89
            $collection->clear();
90
        }
91
92
        $output->writeln('');
93
94
        if ($found === 0) {
95
            $infoStyle = new OutputFormatterStyle('white', 'green');
96
            $output->getFormatter()->setStyle('info', $infoStyle);
97
            $formattedInfoBlock = $formatter->formatBlock(['RESULT:', 'All addresses are valid'], 'info', TRUE);
98
            $output->writeln($formattedInfoBlock);
99
        } else {
100
            $infoStyle = new OutputFormatterStyle('white', 'red');
101
            $output->getFormatter()->setStyle('info', $infoStyle);
102
            $formattedInfoBlock = $formatter->formatBlock(['RESULT:', 'We found ' . array_sum($foundAddresses) . ' addresses with issues and ' . $found . ' fields which do not pass validation. Please update validation regex.'], 'info', TRUE);
103
            $output->writeln($formattedInfoBlock);
104
        }
105
106
        return 0;
0 ignored issues
show
Bug Best Practice introduced by
The expression return 0 returns the type integer which is incompatible with the documented return type void.
Loading history...
107
    }
108
}
109