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 ( a4bd13...c92bdf )
by Andreas
03:29
created

ExportOrderCommand::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
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 16
rs 8.0555
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 Magento\Framework\Console\Cli;
10
use Magento\Framework\MessageQueue\PublisherInterface;
11
use Symfony\Component\Console\Exception\RuntimeException;
12
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...
13
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...
14
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Order\Collection as OrderCollection;
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 ExportOrderCommand
21
 */
22
class ExportOrderCommand extends AbstractExportCommand
23
{
24
    private const NAME = 'activecampaign:export:order';
25
    private const ORDER_ID = 'order-id';
26
    private const OPTION_OMITTED = 'omitted';
27
    private const OPTION_ALL = 'all';
28
29
    /**
30
     * @var OrderCollectionFactory
31
     */
32
    private $orderCollectionFactory;
33
34
    /**
35
     * @param OrderCollectionFactory $orderCollectionFactory
36
     * @param PublisherInterface $publisher
37
     * @param ProgressBarFactory $progressBarFactory
38
     */
39
    public function __construct(
40
        OrderCollectionFactory $orderCollectionFactory,
41
        ProgressBarFactory $progressBarFactory,
42
        PublisherInterface $publisher
43
    ) {
44
        $this->orderCollectionFactory = $orderCollectionFactory;
45
        parent::__construct($progressBarFactory, $publisher);
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    protected function configure()
52
    {
53
        $this->setName(self::NAME)
54
            ->setDescription('Export orders')
55
            ->addOption(
56
                self::ORDER_ID,
57
                null,
58
                InputOption::VALUE_REQUIRED,
59
                'The order id'
60
            )
61
            ->addOption(
62
                self::OPTION_OMITTED,
63
                null,
64
                InputOption::VALUE_NONE,
65
                'Only export omitted orders'
66
            )
67
            ->addOption(
68
                self::OPTION_ALL,
69
                null,
70
                InputOption::VALUE_NONE,
71
                'Export all orders'
72
            );
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    protected function interact(InputInterface $input, OutputInterface $output)
79
    {
80
        $orderId = $input->getOption(self::ORDER_ID);
81
        $omitted = $input->getOption(self::OPTION_OMITTED);
82
        $all = $input->getOption(self::OPTION_ALL);
83
84
        if ($orderId === null && $omitted === false && $all === false) {
85
            throw new RuntimeException('Please provide at least one option');
86
        }
87
88
        if ($orderId !== null && ($omitted === true || $all === true)) {
89
            throw new RuntimeException('You cannot use --order-id 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
        $orderIds = $this->getOrderIds($input);
103
        $orderIdsCount = count($orderIds);
104
105
        if ($orderIdsCount === 0) {
106
            $output->writeln('<error>No order(s) found matching your criteria</error>');
107
            return Cli::RETURN_FAILURE;
108
        }
109
110
        $progressBar = $this->createProgressBar(
111
            $output,
112
            $orderIdsCount,
113
            'Order(s)'
114
        );
115
116
        foreach ($orderIds as $orderId) {
117
            $this->publisher->publish(
118
                Topics::SALES_ORDER_EXPORT,
119
                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

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