1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | /** |
||
4 | */ |
||
5 | |||
6 | namespace CommerceLeague\ActiveCampaign\Console\Command; |
||
7 | |||
8 | use CommerceLeague\ActiveCampaign\MessageQueue\Topics; |
||
9 | use CommerceLeague\ActiveCampaign\Model\ResourceModel\Magento\CustomerCollectionFactory; |
||
10 | use CommerceLeague\ActiveCampaign\Model\ResourceModel\Magento\CustomerCollection; |
||
11 | use Magento\Framework\Console\Cli; |
||
12 | use Magento\Framework\MessageQueue\PublisherInterface; |
||
13 | use CommerceLeague\ActiveCampaign\Model\ResourceModel\Magento\SubscriberCollectionFactory as SubscriberCollectionFactory; |
||
14 | use CommerceLeague\ActiveCampaign\Model\ResourceModel\Magento\SubscriberCollection as SubscriberCollection; |
||
15 | use Magento\Newsletter\Model\Subscriber; |
||
16 | use Symfony\Component\Console\Exception\RuntimeException; |
||
17 | use Symfony\Component\Console\Helper\ProgressBarFactory; |
||
18 | use Symfony\Component\Console\Input\InputInterface; |
||
19 | use Symfony\Component\Console\Input\InputOption; |
||
20 | use Symfony\Component\Console\Output\OutputInterface; |
||
21 | |||
22 | /** |
||
23 | * Class ExportContactCommand |
||
24 | */ |
||
25 | class ExportContactCommand extends AbstractExportCommand |
||
26 | { |
||
27 | private const NAME = 'activecampaign:export:contact'; |
||
28 | private const OPTION_EMAIL = 'email'; |
||
29 | private const OPTION_OMITTED = 'omitted'; |
||
30 | private const OPTION_ALL = 'all'; |
||
31 | |||
32 | /** |
||
33 | * @var CustomerCollectionFactory |
||
34 | */ |
||
35 | private $customerCollectionFactory; |
||
36 | |||
37 | /** |
||
38 | * @var SubscriberCollectionFactory |
||
39 | */ |
||
40 | private $subscriberCollectionFactory; |
||
41 | |||
42 | /** |
||
43 | * @param CustomerCollectionFactory $customerCollectionFactory |
||
44 | * @param SubscriberCollectionFactory $subscriberCollectionFactory |
||
45 | * @param PublisherInterface $publisher |
||
46 | * @param ProgressBarFactory $progressBarFactory |
||
47 | */ |
||
48 | public function __construct( |
||
49 | CustomerCollectionFactory $customerCollectionFactory, |
||
50 | SubscriberCollectionFactory $subscriberCollectionFactory, |
||
51 | PublisherInterface $publisher, |
||
52 | ProgressBarFactory $progressBarFactory |
||
53 | ) { |
||
54 | $this->customerCollectionFactory = $customerCollectionFactory; |
||
55 | $this->subscriberCollectionFactory = $subscriberCollectionFactory; |
||
56 | parent::__construct($progressBarFactory, $publisher); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @inheritDoc |
||
61 | */ |
||
62 | protected function configure() |
||
63 | { |
||
64 | $this->setName(self::NAME) |
||
65 | ->setDescription('Export contacts') |
||
66 | ->addOption( |
||
67 | self::OPTION_EMAIL, |
||
68 | null, |
||
69 | InputOption::VALUE_REQUIRED, |
||
70 | 'The contact email' |
||
71 | ) |
||
72 | ->addOption( |
||
73 | self::OPTION_OMITTED, |
||
74 | null, |
||
75 | InputOption::VALUE_NONE, |
||
76 | 'Only export omitted contacts' |
||
77 | ) |
||
78 | ->addOption( |
||
79 | self::OPTION_ALL, |
||
80 | null, |
||
81 | InputOption::VALUE_NONE, |
||
82 | 'Export all contacts' |
||
83 | ); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @inheritDoc |
||
88 | */ |
||
89 | protected function interact(InputInterface $input, OutputInterface $output) |
||
90 | { |
||
91 | $email = $input->getOption(self::OPTION_EMAIL); |
||
92 | $omitted = $input->getOption(self::OPTION_OMITTED); |
||
93 | $all = $input->getOption(self::OPTION_ALL); |
||
94 | |||
95 | if ($email === null && $omitted === false && $all === false) { |
||
96 | throw new RuntimeException('Please provide at least one option'); |
||
97 | } |
||
98 | |||
99 | if ($email !== null && ($omitted === true || $all === true)) { |
||
100 | throw new RuntimeException('You cannot use --email together with another option'); |
||
101 | } |
||
102 | |||
103 | if ($omitted === true && $all === true) { |
||
104 | throw new RuntimeException('You cannot use --omitted and --all together'); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @inheritDoc |
||
110 | */ |
||
111 | protected function execute(InputInterface $input, OutputInterface $output) |
||
112 | { |
||
113 | $customerIds = $this->getCustomerIds($input); |
||
114 | $customerIdsCount = count($customerIds); |
||
115 | $subscriberEmails = $this->getSubscriberEmails($input); |
||
116 | $subscriberEmailsCount = count($subscriberEmails); |
||
117 | |||
118 | if (($customerIdsCount + $subscriberEmailsCount) === 0) { |
||
119 | $output->writeln('<error>No contact(s) found matching your criteria</error>'); |
||
120 | return Cli::RETURN_FAILURE; |
||
121 | } |
||
122 | |||
123 | if ($customerIdsCount > 0) { |
||
124 | $progressBar = $this->createProgressBar( |
||
125 | $output, |
||
126 | $customerIdsCount, |
||
127 | 'Customer(s)' |
||
128 | ); |
||
129 | |||
130 | foreach ($customerIds as $customerId) { |
||
131 | $this->publisher->publish( |
||
132 | Topics::CUSTOMER_CONTACT_EXPORT, |
||
133 | json_encode(['magento_customer_id' => $customerId]) |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
134 | ); |
||
135 | |||
136 | $progressBar->advance(); |
||
137 | } |
||
138 | |||
139 | $output->writeln(''); |
||
140 | } |
||
141 | |||
142 | if ($subscriberEmailsCount > 0) { |
||
143 | $progressBar = $this->createProgressBar( |
||
144 | $output, |
||
145 | $subscriberEmailsCount, |
||
146 | 'Subscriber(s)' |
||
147 | ); |
||
148 | |||
149 | foreach ($subscriberEmails as $subscriberEmail) { |
||
150 | $this->publisher->publish( |
||
151 | Topics::NEWSLETTER_CONTACT_EXPORT, |
||
152 | json_encode(['email' => $subscriberEmail]) |
||
153 | ); |
||
154 | |||
155 | $progressBar->advance(); |
||
156 | } |
||
157 | |||
158 | $output->writeln(''); |
||
159 | } |
||
160 | |||
161 | $output->writeln(sprintf( |
||
162 | '<info>%s contacts(s) have been scheduled for export.</info>', |
||
163 | ($customerIdsCount + $subscriberEmailsCount)) |
||
164 | ); |
||
165 | |||
166 | return Cli::RETURN_SUCCESS; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param InputInterface $input |
||
171 | * @return array |
||
172 | */ |
||
173 | private function getCustomerIds(InputInterface $input): array |
||
174 | { |
||
175 | /** @var CustomerCollection $customerCollection */ |
||
176 | $customerCollection = $this->customerCollectionFactory->create(); |
||
177 | |||
178 | if (($email = $input->getOption(self::OPTION_EMAIL)) !== null) { |
||
179 | $customerCollection->addEmailFilter($email); |
||
180 | } |
||
181 | |||
182 | if ($input->getOption(self::OPTION_OMITTED)) { |
||
183 | $customerCollection->addContactOmittedFilter(); |
||
184 | } |
||
185 | |||
186 | return $customerCollection->getAllIds(); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param InputInterface $input |
||
191 | * @return array |
||
192 | */ |
||
193 | private function getSubscriberEmails(InputInterface $input): array |
||
194 | { |
||
195 | /** @var SubscriberCollection $subscriberCollection */ |
||
196 | $subscriberCollection = $this->subscriberCollectionFactory->create(); |
||
197 | $subscriberCollection->excludeCustomers(); |
||
198 | |||
199 | if (($email = $input->getOption(self::OPTION_EMAIL)) !== null) { |
||
200 | $subscriberCollection->addEmailFilter($email); |
||
201 | } |
||
202 | |||
203 | if ($input->getOption(self::OPTION_OMITTED)) { |
||
204 | $subscriberCollection->addContactOmittedFilter(); |
||
205 | } |
||
206 | |||
207 | return $subscriberCollection->getAllEmails(); |
||
208 | } |
||
209 | } |
||
210 |