ProgressPanel   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 58
ccs 0
cts 28
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 3
A setProgress() 0 4 1
A setData() 0 8 1
1
<?php
2
3
  namespace Funivan\Console\ProgressPanel;
4
5
  use Symfony\Component\Console\Helper\ProgressBar;
6
  use Symfony\Component\Console\Output\OutputInterface;
7
8
  /**
9
   * Extended progress bar
10
   * Progress bar attached to the bottom of the console
11
   * You can add some data to output by setData method
12
   *
13
   * @author Ivan Shcherbak <[email protected]>
14
   */
15
  class ProgressPanel extends ProgressBar {
16
17
    /**
18
     * Configure progress panel
19
     *
20
     * @param OutputInterface $output
21
     * @param int $max
22
     */
23
    public function __construct(OutputInterface $output, $max = 0) {
24
25
      static::setPlaceholderFormatterDefinition('current', function (ProgressBar $bar) {
26
        return $bar->getProgress();
27
      });
28
29
      parent::__construct($output, $max);
30
31
      if ($max === 0) {
32
        $this->setFormat('%data% %current% [%bar%]  %elapsed:10s% / %memory:-10s%  %itemsPerSecond:-10s% %message%');
33
      } else {
34
        $this->setFormat('%data% %current%/%max% [%bar%] %percent:3s%% %elapsed:10s% / %estimated:-10s% %memory:-10s%  %itemsPerSecond:-10s% %message%');
35
      }
36
      $this->setMessage('', 'data');
37
      $this->setMessage('');
38
39
      static::setPlaceholderFormatterDefinition('itemsPerSecond', function (ProgressBar $bar) {
40
41
        $seconds = (time() - $bar->getStartTime());
42
        $seconds = empty($seconds) ? 1 : $seconds;
43
        return round($bar->getProgress() / $seconds) . ' i/s';
44
      });
45
46
    }
47
48
49
    /**
50
     * @param int $step
51
     * @throws \Symfony\Component\Console\Exception\LogicException
52
     */
53
    public function setProgress($step) {
54
      $this->setMessage('', 'data');
55
      parent::setProgress($step);
56
    }
57
58
59
    /**
60
     * @param string $data
61
     * @return $this
62
     */
63
    public function setData($data) {
64
      $this->clear();
65
      $this->setMessage($data . "\n", 'data');
66
      $this->display();
67
      $this->setMessage('', 'data');
68
69
      return $this;
70
    }
71
72
  }