|
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; |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths