ProgressClosureBuilder::build()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 13
cp 0
rs 9.584
c 0
b 0
f 0
cc 3
nc 1
nop 4
crap 12
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <https://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Command;
13
14
use Symfony\Component\Console\Helper\ProgressBar;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * @internal
19
 */
20
final class ProgressClosureBuilder
21
{
22
    /**
23
     * Builds a loggerClosure to be called from inside the Provider to update the command
24
     * line.
25
     */
26
    public static function build(OutputInterface $output, string $action, string $index, int $offset): \Closure
27
    {
28
        $progress = null;
29
30
        return function ($increment, $totalObjects, $message = null) use (&$progress, $output, $action, $index, $offset) {
31
            if (null === $progress) {
32
                $progress = new ProgressBar($output, $totalObjects);
33
                $progress->start();
34
                $progress->setProgress($offset);
35
            }
36
37
            if (null !== $message) {
38
                $progress->clear();
39
                $output->writeln(\sprintf('<info>%s</info> <error>%s</error>', $action, $message));
40
                $progress->display();
41
            }
42
43
            $progress->setMessage(\sprintf('<info>%s</info> <comment>%s</comment>', $action, $index));
44
            $progress->advance($increment);
45
        };
46
    }
47
}
48