Completed
Pull Request — master (#1331)
by
unknown
05:27
created

ProgressClosureBuilder::buildLegacy()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 35
ccs 0
cts 30
cp 0
rs 8.5806
cc 4
eloc 25
nc 1
nop 5
crap 20
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://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
/**
13
 * This file is part of the FOSElasticaBundle project.
14
 *
15
 * (c) Tim Nagel <[email protected]>
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 */
20
21
namespace FOS\ElasticaBundle\Command;
22
23
use Symfony\Component\Console\Helper\ProgressBar;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
/**
27
 * @internal
28
 */
29
final class ProgressClosureBuilder
30
{
31
    /**
32
     * Builds a loggerClosure to be called from inside the Provider to update the command
33
     * line.
34
     *
35
     * @param OutputInterface $output
36
     * @param string          $action
37
     * @param string          $index
38
     * @param string          $type
39
     * @param int             $offset
40
     *
41
     * @return callable
42
     */
43
    public static function build(OutputInterface $output, $action, $index, $type, $offset)
44
    {
45
        $progress = null;
46
47
        return function ($increment, $totalObjects, $message = null) use (&$progress, $output, $action, $index, $type, $offset) {
48
            if (null === $progress) {
49
                $progress = new ProgressBar($output, $totalObjects);
50
                $progress->start();
51
                $progress->setProgress($offset);
52
            }
53
54
            if (null !== $message) {
55
                $progress->clear();
56
                $output->writeln(sprintf('<info>%s</info> <error>%s</error>', $action, $message));
57
                $progress->display();
58
            }
59
60
            $progress->setMessage(sprintf('<info>%s</info> <comment>%s/%s</comment>', $action, $index, $type));
61
            $progress->advance($increment);
62
        };
63
    }
64
}
65