Issues (3627)

app/bundles/LeadBundle/Helper/Progress.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2015 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\LeadBundle\Helper;
13
14
use Mautic\CoreBundle\Helper\ProgressBarHelper;
15
use Symfony\Component\Console\Helper\ProgressBar;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class Progress
19
{
20
    /**
21
     * Total number of items representing 100%.
22
     *
23
     * @var int
24
     */
25
    protected $total = 0;
26
27
    /**
28
     * Currently proccessed items.
29
     *
30
     * @var int
31
     */
32
    protected $done = 0;
33
34
    /**
35
     * @var OutputInterface|null
36
     */
37
    protected $output;
38
39
    /**
40
     * @var ProgressBar|null
41
     */
42
    protected $bar;
43
44
    public function __construct(OutputInterface $output = null)
45
    {
46
        $this->output = $output;
47
    }
48
49
    /**
50
     * Returns count of all items.
51
     *
52
     * @return int
53
     */
54
    public function getTotal()
55
    {
56
        return $this->total;
57
    }
58
59
    /**
60
     * Set total value.
61
     *
62
     * @param int $total
63
     *
64
     * @return Progress
65
     */
66
    public function setTotal($total)
67
    {
68
        $this->total = (int) $total;
69
70
        if ($this->output) {
71
            $this->bar = ProgressBarHelper::init($this->output, $this->total);
72
            $this->bar->start();
73
        }
74
75
        return $this;
76
    }
77
78
    /**
79
     * Returns count of processed items.
80
     *
81
     * @return int
82
     */
83
    public function getDone()
84
    {
85
        return $this->done;
86
    }
87
88
    /**
89
     * Set total value.
90
     *
91
     * @return Progress
92
     */
93
    public function setDone($done)
94
    {
95
        $this->done = (int) $done;
96
97
        if ($this->bar) {
98
            $this->bar->setProgress($this->done);
99
100
            if ($this->isFinished()) {
101
                $this->bar->finish();
102
                $this->output->writeln('');
0 ignored issues
show
The method writeln() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
                $this->output->/** @scrutinizer ignore-call */ 
103
                               writeln('');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
            }
104
        }
105
106
        return $this;
107
    }
108
109
    /**
110
     * Increase done count by 1.
111
     *
112
     * @return Progress
113
     */
114
    public function increase()
115
    {
116
        $this->setDone($this->done + 1);
117
118
        return $this;
119
    }
120
121
    /**
122
     * Checked if the progress is 100 or more %.
123
     *
124
     * @return bool
125
     */
126
    public function isFinished()
127
    {
128
        return $this->done >= $this->total;
129
    }
130
131
    /**
132
     * Bind Progress from simple array.
133
     *
134
     * @return Progress
135
     */
136
    public function bindArray(array $progress)
137
    {
138
        if (isset($progress[0])) {
139
            $this->setDone($progress[0]);
140
        }
141
142
        if (isset($progress[1])) {
143
            $this->setTotal($progress[1]);
144
        }
145
146
        return $this;
147
    }
148
149
    /**
150
     * Convert this object to a simple array.
151
     *
152
     * @return array
153
     */
154
    public function toArray()
155
    {
156
        return [
157
            $this->done,
158
            $this->total,
159
        ];
160
    }
161
162
    /**
163
     * Counts percentage of the progress.
164
     *
165
     * @return int
166
     */
167
    public function toPercent()
168
    {
169
        return ($this->total) ? ceil(($this->done / $this->total) * 100) : 100;
170
    }
171
}
172