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.

ExportOrderCommand::interact()   B
last analyzed

Complexity

Conditions 11
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 11
nc 5
nop 2
dl 0
loc 20
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
/**
4
 */
5
6
namespace CommerceLeague\ActiveCampaign\Console\Command;
7
8
use CommerceLeague\ActiveCampaign\Helper\Config as ConfigHelper;
9
use CommerceLeague\ActiveCampaign\MessageQueue\Topics;
10
use Magento\Framework\Console\Cli;
11
use Magento\Framework\MessageQueue\PublisherInterface;
12
use Symfony\Component\Console\Exception\RuntimeException;
13
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...
14
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
0 ignored issues
show
Bug introduced by
The type CommerceLeague\ActiveCam...Order\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...
15
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Order\Collection as OrderCollection;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * Class ExportOrderCommand
22
 */
23
class ExportOrderCommand extends AbstractExportCommand
24
{
25
    private const NAME = 'activecampaign:export:order';
26
    private const ORDER_ID = 'order-id';
27
    private const OPTION_OMITTED = 'omitted';
28
    private const OPTION_ALL = 'all';
29
30
    /**
31
     * @var OrderCollectionFactory
32
     */
33
    private $orderCollectionFactory;
34
35
    /**
36
     * @param ConfigHelper $configHelper
37
     * @param OrderCollectionFactory $orderCollectionFactory
38
     * @param ProgressBarFactory $progressBarFactory
39
     * @param PublisherInterface $publisher
40
     */
41
    public function __construct(
42
        ConfigHelper $configHelper,
43
        OrderCollectionFactory $orderCollectionFactory,
44
        ProgressBarFactory $progressBarFactory,
45
        PublisherInterface $publisher
46
    ) {
47
        $this->orderCollectionFactory = $orderCollectionFactory;
48
        parent::__construct($configHelper, $progressBarFactory, $publisher);
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    protected function configure()
55
    {
56
        $this->setName(self::NAME)
57
            ->setDescription('Export orders')
58
            ->addOption(
59
                self::ORDER_ID,
60
                null,
61
                InputOption::VALUE_REQUIRED,
62
                'The order id'
63
            )
64
            ->addOption(
65
                self::OPTION_OMITTED,
66
                null,
67
                InputOption::VALUE_NONE,
68
                'Only export omitted orders'
69
            )
70
            ->addOption(
71
                self::OPTION_ALL,
72
                null,
73
                InputOption::VALUE_NONE,
74
                'Export all orders'
75
            );
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    protected function interact(InputInterface $input, OutputInterface $output)
82
    {
83
        if (!$this->configHelper->isEnabled() || !$this->configHelper->isOrderExportEnabled()) {
84
            throw new RuntimeException('Export disabled by system configuration');
85
        }
86
87
        $orderId = $input->getOption(self::ORDER_ID);
88
        $omitted = $input->getOption(self::OPTION_OMITTED);
89
        $all = $input->getOption(self::OPTION_ALL);
90
91
        if ($orderId === null && $omitted === false && $all === false) {
92
            throw new RuntimeException('Please provide at least one option');
93
        }
94
95
        if ($orderId !== null && ($omitted === true || $all === true)) {
96
            throw new RuntimeException('You cannot use --order-id together with another option');
97
        }
98
99
        if ($omitted === true && $all === true) {
100
            throw new RuntimeException('You cannot use --omitted and --all together');
101
        }
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    protected function execute(InputInterface $input, OutputInterface $output)
108
    {
109
        $orderIds = $this->getOrderIds($input);
110
        $orderIdsCount = count($orderIds);
111
112
        if ($orderIdsCount === 0) {
113
            $output->writeln('<error>No order(s) found matching your criteria</error>');
114
            return Cli::RETURN_FAILURE;
115
        }
116
117
        $progressBar = $this->createProgressBar(
118
            $output,
119
            $orderIdsCount,
120
            'Order(s)'
121
        );
122
123
        foreach ($orderIds as $orderId) {
124
            $this->publisher->publish(
125
                Topics::SALES_ORDER_EXPORT,
126
                json_encode(['magento_order_id' => $orderId])
0 ignored issues
show
Bug introduced by
json_encode(array('magen...order_id' => $orderId)) 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

126
                /** @scrutinizer ignore-type */ json_encode(['magento_order_id' => $orderId])
Loading history...
127
            );
128
129
            $progressBar->advance();
130
        }
131
132
        $output->writeln('');
133
        $output->writeln(sprintf(
134
            '<info>%s order(s) have been scheduled for export.</info>',
135
            ($orderIdsCount)
136
        ));
137
138
        return Cli::RETURN_SUCCESS;
139
    }
140
141
    /**
142
     * @param InputInterface $input
143
     * @return array
144
     */
145
    public function getOrderIds(InputInterface $input): array
146
    {
147
        /** @var OrderCollection $orderCollection */
148
        $orderCollection = $this->orderCollectionFactory->create();
149
        $orderCollection->addExcludeGuestFilter();
150
151
        if (($orderId = $input->getOption(self::ORDER_ID))) {
152
            $orderCollection->addIdFilter((int)$orderId);
153
        }
154
155
        if ($input->getOption(self::OPTION_OMITTED)) {
156
            $orderCollection->addOmittedFilter();
157
        }
158
159
        return $orderCollection->getAllIds();
160
    }
161
}
162