ProgressWriter::finish()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Port\SymfonyConsole;
4
5
use Port\Reader\CountableReader;
6
use Port\Writer;
7
use Symfony\Component\Console\Helper\ProgressBar;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Writes output to the Symfony2 console
12
 *
13
 * @author David de Boer <[email protected]>
14
 */
15
class ProgressWriter implements Writer
16
{
17
    /**
18
     * @var OutputInterface
19
     */
20
    protected $output;
21
22
    /**
23
     * @var ProgressBar
24
     */
25
    protected $progress;
26
27
    /**
28
     * @var string
29
     */
30
    protected $verbosity;
31
32
    /**
33
     * @var CountableReader
34
     */
35
    protected $reader;
36
37
    /**
38
     * @var integer
39
     */
40
    protected $redrawFrequency;
41
42
    /**
43
     * @param OutputInterface $output
44
     * @param CountableReader $reader
45
     * @param string          $verbosity
46
     * @param integer         $redrawFrequency
47
     */
48
    public function __construct(
49
        OutputInterface $output,
50
        CountableReader $reader,
51
        $verbosity = 'debug',
52
        $redrawFrequency = 1
53
    ) {
54
        $this->output           = $output;
55
        $this->reader           = $reader;
56
        $this->verbosity        = $verbosity;
57
        $this->redrawFrequency  = $redrawFrequency;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function prepare()
64
    {
65
        $this->progress = new ProgressBar($this->output, $this->reader->count());
66
        $this->progress->setFormat($this->verbosity);
67
        $this->progress->setRedrawFrequency($this->redrawFrequency);
68
        $this->progress->start();
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function writeItem(array $item)
75
    {
76
        $this->progress->advance();
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function finish()
83
    {
84
        $this->progress->finish();
85
    }
86
}
87