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.
Passed
Push — master ( acc64b...81648c )
by Andreas
03:48
created

ExportCustomerCommand::interact()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 9
nc 4
nop 2
dl 0
loc 16
rs 8.0555
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type CommerceLeague\ActiveCam...stomerCollectionFactory 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...
10
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Magento\CustomerCollection;
11
use Magento\Framework\Console\Cli;
12
use Magento\Framework\MessageQueue\PublisherInterface;
13
use Symfony\Component\Console\Exception\RuntimeException;
14
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...
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Class ExportCustomerCommand
21
 */
22
class ExportCustomerCommand extends AbstractExportCommand
23
{
24
    private const NAME = 'activecampaign:export:customer';
25
    private const OPTION_EMAIL = 'email';
26
    private const OPTION_OMITTED = 'omitted';
27
    private const OPTION_ALL = 'all';
28
29
    /**
30
     * @var CustomerCollectionFactory
31
     */
32
    private $customerCollectionFactory;
33
34
    /**
35
     * @param CustomerCollectionFactory $customerCollectionFactory
36
     * @param PublisherInterface $publisher
37
     * @param ProgressBarFactory $progressBarFactory
38
     */
39
    public function __construct(
40
        CustomerCollectionFactory $customerCollectionFactory,
41
        PublisherInterface $publisher,
42
        ProgressBarFactory $progressBarFactory
43
    ) {
44
        $this->customerCollectionFactory = $customerCollectionFactory;
45
        parent::__construct($progressBarFactory, $publisher);
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    protected function configure()
52
    {
53
        $this->setName(self::NAME)
54
            ->setDescription('Export customers')
55
            ->addOption(
56
                self::OPTION_EMAIL,
57
                null,
58
                InputOption::VALUE_REQUIRED,
59
                'The customer email'
60
            )
61
            ->addOption(
62
                self::OPTION_OMITTED,
63
                null,
64
                InputOption::VALUE_NONE,
65
                'Only export omitted customers'
66
            )
67
            ->addOption(
68
                self::OPTION_ALL,
69
                null,
70
                InputOption::VALUE_NONE,
71
                'Export all customers'
72
            );
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    protected function interact(InputInterface $input, OutputInterface $output)
79
    {
80
        $email = $input->getOption(self::OPTION_EMAIL);
81
        $omitted = $input->getOption(self::OPTION_OMITTED);
82
        $all = $input->getOption(self::OPTION_ALL);
83
84
        if ($email === null && $omitted === false && $all === false) {
85
            throw new RuntimeException('Please provide at least one option');
86
        }
87
88
        if ($email !== null && ($omitted === true || $all === true)) {
89
            throw new RuntimeException('You cannot use --email together with another option');
90
        }
91
92
        if ($omitted === true && $all === true) {
93
            throw new RuntimeException('You cannot use --omitted and --all together');
94
        }
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    protected function execute(InputInterface $input, OutputInterface $output)
101
    {
102
        $customerIds = $this->getCustomerIds($input);
103
        $customerIdsCount = count($customerIds);
104
105
        if ($customerIdsCount === 0) {
106
            $output->writeln('<error>No customer(s) found matching your criteria</error>');
107
            return Cli::RETURN_FAILURE;
108
        }
109
110
        $progressBar = $this->createProgressBar(
111
            $output,
112
            count($customerIds),
113
            'Customer(s)'
114
        );
115
116
        foreach ($customerIds as $customerId) {
117
            $this->publisher->publish(
118
                Topics::CUSTOMER_CUSTOMER_EXPORT,
119
                json_encode(['magento_customer_id' => $customerId])
0 ignored issues
show
Bug introduced by
json_encode(array('magen...er_id' => $customerId)) of type string is incompatible with the type array|object expected by parameter $data of Magento\Framework\Messag...herInterface::publish(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
                /** @scrutinizer ignore-type */ json_encode(['magento_customer_id' => $customerId])
Loading history...
120
            );
121
122
            $progressBar->advance();
123
        }
124
125
        $output->writeln('');
126
        $output->writeln(sprintf(
127
                '<info>%s customers(s) have been scheduled for export.</info>',
128
                (count($customerIds)))
129
        );
130
131
        return Cli::RETURN_SUCCESS;
132
    }
133
134
    /**
135
     * @param InputInterface $input
136
     * @return array
137
     */
138
    private function getCustomerIds(InputInterface $input): array
139
    {
140
        /** @var CustomerCollection $customerCollection */
141
        $customerCollection = $this->customerCollectionFactory->create();
142
143
        if (($email = $input->getOption(self::OPTION_EMAIL)) !== null) {
144
            $customerCollection->addEmailFilter($email);
145
        }
146
147
        if ($input->getOption(self::OPTION_OMITTED)) {
148
            $customerCollection->addOmittedFilter();
149
        }
150
151
        return $customerCollection->getAllIds();
152
    }
153
}
154