GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( bd8b96...8bf920 )
by Andreas
03:51
created

ExportContactCommand::execute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 40
rs 9.584
1
<?php
2
declare(strict_types=1);
3
/**
4
 */
5
6
namespace CommerceLeague\ActiveCampaign\Console\Command;
7
8
use CommerceLeague\ActiveCampaign\Service\ExportContactService;
9
use Magento\Customer\Model\Customer;
10
use Magento\Customer\Model\MagentoCustomerFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Customer\Model\MagentoCustomerFactory 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...
11
use Magento\Customer\Model\ResourceModel\Customer\Collection as MagentoCustomerCollection;
12
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as MagentoCustomerCollectionFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Customer\Model\R...tomer\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...
13
use Magento\Framework\Console\Cli;
14
use Magento\Framework\Model\ResourceModel\Iterator as ResourceIterator;
15
use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory as SubscriberCollectionFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Newsletter\Model...riber\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...
16
use Magento\Newsletter\Model\ResourceModel\Subscriber\Collection as SubscriberCollection;
17
use Magento\Newsletter\Model\Subscriber;
18
use Magento\Newsletter\Model\SubscriberFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Newsletter\Model\SubscriberFactory 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...
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Helper\ProgressBar;
21
use Symfony\Component\Console\Helper\ProgressBarFactory;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Consol...lper\ProgressBarFactory 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...
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
/**
26
 * Class ExportContactCommand
27
 */
28
class ExportContactCommand extends Command
29
{
30
    private const NAME = 'activecampaign:export:contact';
31
32
    /**
33
     * @var MagentoCustomerCollectionFactory
34
     */
35
    private $magentoCustomerCollectionFactory;
36
37
    /**
38
     * @var SubscriberCollectionFactory
39
     */
40
    private $subscriberCollectionFactory;
41
42
    /**
43
     * @var MagentoCustomerFactory
44
     */
45
    private $magentoCustomerFactory;
46
47
    /**
48
     * @var SubscriberFactory
49
     */
50
    private $subscriberFactory;
51
52
    /**
53
     * @var ProgressBarFactory
54
     */
55
    private $progressBarFactory;
56
57
    /**
58
     * @var ResourceIterator
59
     */
60
    private $resourceIterator;
61
62
    /**
63
     * @var ExportContactService
64
     */
65
    private $exportContactService;
66
67
    /**
68
     * @var array
69
     */
70
    private $processedEmails = [];
71
72
    /**
73
     * @var int
74
     */
75
    private $exportMessages = 0;
76
77
    /**
78
     * @param MagentoCustomerCollectionFactory $magentoCustomerCollectionFactory
79
     * @param SubscriberCollectionFactory $subscriberCollectionFactory
80
     * @param MagentoCustomerFactory $magentoCustomerFactory
81
     * @param SubscriberFactory $subscriberFactory
82
     * @param ProgressBarFactory $progressBarFactory
83
     * @param ResourceIterator $resourceIterator
84
     * @param ExportContactService $exportContactService
85
     */
86
    public function __construct(
87
        MagentoCustomerCollectionFactory $magentoCustomerCollectionFactory,
88
        SubscriberCollectionFactory $subscriberCollectionFactory,
89
        MagentoCustomerFactory $magentoCustomerFactory,
90
        SubscriberFactory $subscriberFactory,
91
        ProgressBarFactory $progressBarFactory,
92
        ResourceIterator $resourceIterator,
93
        ExportContactService $exportContactService
94
    ) {
95
        $this->magentoCustomerCollectionFactory = $magentoCustomerCollectionFactory;
96
        $this->subscriberCollectionFactory = $subscriberCollectionFactory;
97
        $this->magentoCustomerFactory = $magentoCustomerFactory;
98
        $this->subscriberFactory = $subscriberFactory;
99
        $this->progressBarFactory = $progressBarFactory;
100
        $this->resourceIterator = $resourceIterator;
101
        $this->exportContactService = $exportContactService;
102
        parent::__construct();
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108
    protected function configure()
109
    {
110
        $this->setName(self::NAME);
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    protected function execute(InputInterface $input, OutputInterface $output)
117
    {
118
        /** @var MagentoCustomerCollection $magentoCustomerCollection */
119
        $magentoCustomerCollection = $this->magentoCustomerCollectionFactory->create();
120
121
        /** @var SubscriberCollection $subscriberCollection */
122
        $subscriberCollection = $this->subscriberCollectionFactory->create();
123
124
        if ($magentoCustomerCollection->getSize() === 0 && $subscriberCollection->getSize() === 0) {
125
            $output->writeln('<error>No valid magento customers or subscribers found</error>');
126
            return Cli::RETURN_FAILURE;
127
        }
128
129
        /** @var ProgressBar $progressBar */
130
        $progressBar = $this->progressBarFactory->create(['output' => $output]);
131
        $progressBar->setFormat('<comment>%message%</comment> %current% [%bar%] %elapsed%');
132
        $progressBar->setMessage('Export Message(s)');
133
134
        $this->resourceIterator->walk(
135
            $magentoCustomerCollection->getSelect(),
136
            [[$this, 'callbackExportMagentoCustomer']],
137
            [
138
                'magentoCustomer' => $this->magentoCustomerFactory->create(),
139
                'progressBar' => $progressBar
140
            ]
141
        );
142
143
        $this->resourceIterator->walk(
144
            $magentoCustomerCollection->getSelect(),
145
            [[$this, 'callbackExportSubscriber']],
146
            [
147
                'subscriber' => $this->subscriberFactory->create(),
148
                'progressBar' => $progressBar
149
            ]
150
        );
151
152
        $output->writeln('');
153
        $output->writeln('<info>Created ' . $this->exportMessages . ' export message(s)</info>');
154
155
        return Cli::RETURN_SUCCESS;
156
    }
157
158
    /**
159
     * @param array $args
160
     */
161
    public function callbackExportMagentoCustomer(array $args): void
162
    {
163
        /** @var Customer $magentoCustomer */
164
        $magentoCustomer = clone $args['magentoCustomer'];
165
        $magentoCustomer->setData($args['row']);
166
167
        /** @var ProgressBar $progressBar */
168
        $progressBar = $args['progressBar'];
169
        $progressBar->advance();
170
171
        $this->exportContactService->exportWithMagentoCustomer($magentoCustomer);
172
        $this->processedEmails[$magentoCustomer->getData('email')] = null;
173
        $this->exportMessages++;
174
    }
175
176
    /**
177
     * @param array $args
178
     */
179
    public function callbackExportSubscriber(array $args): void
180
    {
181
        /** @var Subscriber $subscriber */
182
        $subscriber = clone $args['subscriber'];
183
        $subscriber->setData($args['row']);
184
185
        if (array_key_exists($subscriber->getData('email'), $this->processedEmails)) {
186
            return;
187
        }
188
189
        /** @var ProgressBar $progressBar */
190
        $progressBar = $args['progressBar'];
191
        $progressBar->advance();
192
193
        $this->exportContactService->exportWithSubscriber($subscriber);
194
        $this->exportMessages++;
195
    }
196
}
197