Completed
Push — master ( 301cff...631b6f )
by Peter
18:51
created

Export::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\Console\Progress;
11
12
use Symfony\Component\Console\Helper\ProgressHelper;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use AnimeDb\Bundle\CatalogBundle\Console\Output\Export as ExportOutput;
15
16
/**
17
 * Export ProgressHelper.
18
 *
19
 * @author  Peter Gribanov <[email protected]>
20
 */
21
class Export extends PresetOutput
22
{
23
    /**
24
     * @var ExportOutput
25
     */
26
    protected $output;
27
28
    /**
29
     * @var int
30
     */
31
    private $max = 0;
32
33
    /**
34
     * @var int
35
     */
36
    private $current = 0;
37
38
    /**
39
     * @param ProgressHelper $progress
40
     * @param OutputInterface $output
41
     * @param string $filename
42
     */
43 1
    public function __construct(ProgressHelper $progress, OutputInterface $output, $filename)
44
    {
45 1
        $progress->setFormat(ProgressHelper::FORMAT_QUIET);
46 1
        $output = new ExportOutput($output, $filename, false);
47
        // reset old value
48 1
        $output->write('0%');
49
50 1
        parent::__construct($progress, $output);
51 1
    }
52
53
    /**
54
     * Starts the progress output.
55
     *
56
     * @param int|null $max Maximum steps
57
     */
58
    public function start($max = null)
59
    {
60
        $this->max = (int) $max;
61
        $this->progress->start($this->output, $max);
62
    }
63
64
    /**
65
     * Advances the progress output X steps.
66
     *
67
     * @param int $step Number of steps to advance
68
     * @param bool $redraw Whether to redraw or not
69
     *
70
     * @throws \LogicException
71
     */
72
    public function advance($step = 1, $redraw = false)
73
    {
74
        parent::advance($step, $redraw);
75
76
        $this->current += $step;
77
78
        $percent = 0;
79
        if ($this->max > 0) {
80
            $percent = (float) $this->current / $this->max;
81
        }
82
        $this->output->write(sprintf('%d%%', floor($percent * 100)));
83
    }
84
85 1
    public function __destruct()
86
    {
87
        // say that scanning is completed
88 1
        $this->output->write('100%');
89 1
        $this->output->unlock();
90 1
    }
91
}
92