StartCliReportingService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 138
Duplicated Lines 28.26 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 39
loc 138
c 0
b 0
f 0
wmc 7
lcom 1
cbo 7
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 17 17 1
A configure() 0 10 1
A execute() 13 13 1
A startProcesses() 9 9 2
A createProcessDefinition() 0 9 1
A getFullCommand() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * File: StartCliReportingService.php
6
 *
7
 * @author      Maciej Sławik <[email protected]>
8
 * Github:      https://github.com/maciejslawik
9
 */
10
11
namespace MSlwk\ReactPhpPlayground\Console\Command;
12
13
use MSlwk\ReactPhpPlayground\Api\ChunkSizeCalculatorInterface;
14
use MSlwk\ReactPhpPlayground\Api\CustomerIdsProviderInterface;
15
use MSlwk\ReactPhpPlayground\Api\TimerInterface;
16
use MSlwk\ReactPhpPlayground\Model\Adapter\ReactPHP\ProcessFactory;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Magento\Framework\Serialize\Serializer\Json;
22
use React\EventLoop\Factory;
23
24
/**
25
 * Class StartCliReportingService
26
 * @package MSlwk\ReactPhpPlayground\Console\Command
27
 */
28
class StartCliReportingService extends Command
29
{
30
    const COMMAND_NAME = 'mslwk:cli-reporting-start';
31
    const COMMAND_DESCRIPTION = 'Start asynchronous CLI reporting service';
32
    const ARGUMENT_NUMBER_OF_THREADS = 'threads';
33
34
    /**
35
     * @var TimerInterface
36
     */
37
    private $timer;
38
39
    /**
40
     * @var CustomerIdsProviderInterface
41
     */
42
    private $customerIdsProvider;
43
44
    /**
45
     * @var LoopInterface
46
     */
47
    private $loop;
48
49
    /**
50
     * @var ProcessFactory
51
     */
52
    private $processFactory;
53
54
    /**
55
     * @var Json
56
     */
57
    private $jsonHandler;
58
59
    /**
60
     * @var ChunkSizeCalculatorInterface
61
     */
62
    private $chunkSizeCalculator;
63
64
    /**
65
     * StartCliReportingService constructor.
66
     * @param TimerInterface $timer
67
     * @param CustomerIdsProviderInterface $customerIdsProvider
68
     * @param Factory $loopFactory
69
     * @param ProcessFactory $processFactory
70
     * @param Json $jsonHandler
71
     * @param ChunkSizeCalculatorInterface $chunkSizeCalculator
72
     * @param null $name
73
     */
74 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
        TimerInterface $timer,
76
        CustomerIdsProviderInterface $customerIdsProvider,
77
        Factory $loopFactory,
78
        ProcessFactory $processFactory,
79
        Json $jsonHandler,
80
        ChunkSizeCalculatorInterface $chunkSizeCalculator,
81
        $name = null
82
    ) {
83
        parent::__construct($name);
84
        $this->timer = $timer;
85
        $this->customerIdsProvider = $customerIdsProvider;
86
        $this->loop = $loopFactory::create();
0 ignored issues
show
Documentation Bug introduced by
It seems like $loopFactory::create() of type object<React\EventLoop\LoopInterface> is incompatible with the declared type object<MSlwk\ReactPhpPla...\Command\LoopInterface> of property $loop.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87
        $this->processFactory = $processFactory;
88
        $this->jsonHandler = $jsonHandler;
89
        $this->chunkSizeCalculator = $chunkSizeCalculator;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    protected function configure()
96
    {
97
        $this->setName(self::COMMAND_NAME)
98
            ->setDescription(self::COMMAND_DESCRIPTION)
99
            ->addArgument(
100
                self::ARGUMENT_NUMBER_OF_THREADS,
101
                InputArgument::REQUIRED,
102
                'Number of threads for running the export process'
103
            );
104
    }
105
106
    /**
107
     * @param InputInterface $input
108
     * @param OutputInterface $output
109
     * @return void
110
     */
111 View Code Duplication
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113
        $numberOfThreads = (int)$input->getArgument(self::ARGUMENT_NUMBER_OF_THREADS);
114
        $customerIds = $this->customerIdsProvider->getCustomerIds();
115
116
        $this->timer->startTimer();
117
118
        $this->startProcesses($customerIds, $numberOfThreads);
119
120
        $this->timer->stopTimer();
121
122
        $output->writeln("<info>Process finished after {$this->timer->getExecutionTimeInSeconds()} seconds</info>");
123
    }
124
125
    /**
126
     * @param int[] $customerIds
127
     * @param int $numberOfThreads
128
     */
129 View Code Duplication
    protected function startProcesses(array $customerIds, int $numberOfThreads): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        $chunkSize = $this->chunkSizeCalculator->calculateChunkSize($customerIds, $numberOfThreads);
132
        $threadedCustomerIds = array_chunk($customerIds, $chunkSize);
133
        foreach ($threadedCustomerIds as $customerIdsForSingleThread) {
134
            $this->createProcessDefinition($this->getFullCommand($customerIdsForSingleThread));
135
        }
136
        $this->loop->run();
137
    }
138
139
    /**
140
     * @param string $command
141
     */
142
    protected function createProcessDefinition(string $command): void
143
    {
144
        $reactProcess = $this->processFactory->create($command);
145
        $reactProcess->start($this->loop);
146
147
        $reactProcess->stdout->on('data', function ($chunk) {
148
            echo $chunk;
149
        });
150
    }
151
152
    /**
153
     * @param int[] $customerIds
154
     * @return string
155
     */
156
    protected function getFullCommand(array $customerIds): string
157
    {
158
        return sprintf(
159
            ' %s/bin/magento %s %s',
160
            BP,
161
            GenerateReports::COMMAND_NAME,
162
            $this->jsonHandler->serialize($customerIds)
163
        );
164
    }
165
}
166