Completed
Pull Request — master (#12)
by Harry
02:36
created

Table::updateRowKeyLengths()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 8.8571
cc 5
eloc 7
nc 3
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Table::formatRow() 0 6 2
1
<?php
2
/**
3
 * This file is part of graze/parallel-process.
4
 *
5
 * Copyright (c) 2017 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/parallel-process/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/parallel-process
12
 */
13
14
namespace Graze\ParallelProcess;
15
16
use Exception;
17
use Graze\DiffRenderer\DiffConsoleOutput;
18
use Graze\DiffRenderer\Terminal\TerminalInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Process\Exception\ProcessFailedException;
21
use Symfony\Component\Process\Process;
22
23
class Table
24
{
25
    use TagsTrait;
26
27
    const SPINNER = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏";
28
29
    /** @var Pool */
30
    private $processPool;
31
    /** @var string[] */
32
    private $rows = [];
33
    /** @var Exception[] */
34
    private $exceptions;
35
    /** @var DiffConsoleOutput */
36
    private $output;
37
    /** @var TerminalInterface */
38
    private $terminal;
39
    /** @var bool */
40
    private $showOutput = true;
41
    /** @var bool */
42
    private $showSummary = true;
43
44
    /**
45
     * Table constructor.
46
     *
47
     * @param OutputInterface $output
48
     * @param Pool|null       $pool
49
     */
50 15 View Code Duplication
    public function __construct(OutputInterface $output, Pool $pool = null)
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...
51
    {
52 15
        $this->processPool = $pool ?: new Pool();
53 15
        if (!$output instanceof DiffConsoleOutput) {
54 3
            $this->output = new DiffConsoleOutput($output);
55 3
            $this->output->setTrim(true);
56 3
        } else {
57 15
            $this->output = $output;
58
        }
59 15
        $this->terminal = $this->output->getTerminal();
60 15
        $this->exceptions = [];
61 15
    }
62
63
    /**
64
     * @param array  $data
65
     * @param string $status
66
     * @param float  $duration
67
     * @param string $extra
68
     *
69
     * @return string
70
     */
71 12
    private function formatRow(array $data, $status, $duration, $extra = '')
72
    {
73 12
        $tags = $this->formatTags($data);
74 12
        $extra = $extra ? '  ' . $this->terminal->filter($extra) : '';
75 12
        return sprintf("%s (<comment>%6.2fs</comment>) %s%s", $tags, $duration, $status, $extra);
76
    }
77
78
    /**
79
     * @param Process $process
80
     * @param array   $data
81
     */
82 12
    public function add(Process $process, array $data = [])
83
    {
84 12
        $index = count($this->rows);
85 12
        $this->rows[$index] = $this->formatRow($data, '', 0);
86 12
        $spinner = 0;
87
88 12
        if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
89
            $onProgress = function ($process, $duration, $last) use ($index, $data, &$spinner) {
90 9
                $this->rows[$index] = $this->formatRow(
91 9
                    $data,
92 9
                    mb_substr(static::SPINNER, $spinner++, 1),
93 9
                    $duration,
94 9
                    ($this->showOutput ? $last : '')
95 9
                );
96 9
                if ($spinner > mb_strlen(static::SPINNER) - 1) {
97 1
                    $spinner = 0;
98 1
                }
99 9
                $this->render();
100 9
            };
101 9
        } else {
102 3
            $onProgress = null;
103
        }
104
105 12
        $run = new Run(
106 12
            $process,
107
            function ($process, $duration, $last) use ($index, $data) {
108 10
                $this->rows[$index] = $this->formatRow($data, "<info>✓</info>", $duration, $last);
109 10
                $this->render($index);
110 12
            },
111 12
            function ($process, $duration, $last) use ($index, $data) {
112 3
                $this->rows[$index] = $this->formatRow($data, "<error>x</error>", $duration, $last);
113 3
                $this->render($index);
114 3
                $this->exceptions[] = new ProcessFailedException($process);
115 12
            },
116
            $onProgress
117 12
        );
118 12
        $run->setUpdateOnProcessOutput(false);
119 12
        $this->processPool->add($run);
120
121 12
        $this->updateRowKeyLengths($data);
122 12
    }
123
124
    /**
125
     * @return string
126
     */
127 2
    private function getSummary()
128
    {
129 2
        if ($this->processPool->hasStarted()) {
130 2
            if ($this->processPool->isRunning()) {
131 2
                return sprintf(
132 2
                    '<comment>Total</comment>: %2d, <comment>Running</comment>: %2d, <comment>Waiting</comment>: %2d',
133 2
                    $this->processPool->count(),
134 2
                    count($this->processPool->getRunning()),
135 2
                    count($this->processPool->getWaiting())
136 2
                );
137
            } else {
138 2
                return '';
139
            }
140
        } else {
141 1
            return 'waiting...';
142
        }
143
    }
144
145
    /**
146
     * Render a specific row
147
     *
148
     * @param int $row
149
     */
150 12
    private function render($row = 0)
151
    {
152 12
        if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
153 9
            $rows = ($this->showSummary ? array_merge($this->rows, [$this->getSummary()]) : $this->rows);
154 9
            $this->output->reWrite($rows, !$this->showSummary);
155 9
        } else {
156 3
            $this->output->writeln($this->rows[$row]);
157
        }
158 12
    }
159
160
    /**
161
     * @param float $checkInterval
162
     *
163
     * @return bool true if all processes were successful
164
     * @throws Exception
165
     */
166 12
    public function run($checkInterval = Pool::CHECK_INTERVAL)
167
    {
168 12
        if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
169 9
            $this->render();
170 9
        }
171 12
        $output = $this->processPool->run($checkInterval);
172 12
        if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE && $this->showSummary) {
173 2
            $this->render();
174 2
        }
175
176 12
        if (count($this->exceptions) > 0) {
177 3
            foreach ($this->exceptions as $exception) {
178 3
                $this->output->writeln($exception->getMessage());
179 3
            }
180
181 3
            throw reset($this->exceptions);
182
        }
183
184 9
        return $output;
185
    }
186
187
    /**
188
     * @return bool
189
     */
190 1
    public function isShowOutput()
191
    {
192 1
        return $this->showOutput;
193
    }
194
195
    /**
196
     * @param bool $showOutput
197
     *
198
     * @return $this
199
     */
200 11
    public function setShowOutput($showOutput)
201
    {
202 11
        $this->showOutput = $showOutput;
203 11
        return $this;
204
    }
205
206
    /**
207
     * @return bool
208
     */
209 1
    public function isShowSummary()
210
    {
211 1
        return $this->showSummary;
212
    }
213
214
    /**
215
     * @param bool $showSummary
216
     *
217
     * @return $this
218
     */
219 13
    public function setShowSummary($showSummary)
220
    {
221 13
        $this->showSummary = $showSummary;
222 13
        return $this;
223
    }
224
}
225