Completed
Push — master ( 7afd8d...749251 )
by Fabrice
02:41
created

ProgressBarSubscriber::setProgressMod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of YaEtl.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Laravel\Callbacks;
11
12
use fab2s\NodalFlow\Events\FlowEvent;
13
use fab2s\NodalFlow\Events\FlowEventInterface;
14
use fab2s\NodalFlow\Flows\FlowInterface;
15
use Symfony\Component\Console\Helper\ProgressBar;
16
use Symfony\Component\Console\Output\ConsoleOutput;
17
use Symfony\Component\Console\Output\ConsoleOutputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
21
/**
22
 * Class ProgressBarSubscriber
23
 */
24
class ProgressBarSubscriber implements EventSubscriberInterface
25
{
26
    /**
27
     * The Laravel output object, extracted from the command object
28
     *
29
     * @var ConsoleOutputInterface
30
     */
31
    protected $output;
32
33
    /**
34
     * The number of records
35
     *
36
     * @var int|null
37
     */
38
    protected $numRecords;
39
40
    /**
41
     * Progress modulo, should be aligned with YaEtl's one
42
     *
43
     * @var int
44
     */
45
    protected $progressMod = 1;
46
47
    /**
48
     * @var ProgressBar
49
     */
50
    protected $progressBar;
51
52
    /**
53
     * @return OutputInterface
54
     */
55
    public function getOutput()
56
    {
57
        if (!isset($this->output)) {
58
            $this->output = new ConsoleOutput;
59
        }
60
61
        return $this->output;
62
    }
63
64
    /**
65
     * @param OutputInterface $output
66
     *
67
     * @return ProgressBarSubscriber
68
     */
69
    public function setOutput(OutputInterface $output)
70
    {
71
        $this->output = $output;
0 ignored issues
show
Documentation Bug introduced by
$output is of type Symfony\Component\Console\Output\OutputInterface, but the property $output was declared to be of type Symfony\Component\Consol...\ConsoleOutputInterface. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
72
73
        return $this;
74
    }
75
76
    /**
77
     * Set progress modulo
78
     *
79
     * @param int $progressMod
80
     *
81
     * @return $this
82
     */
83
    public function setProgressMod($progressMod)
84
    {
85
        $this->progressMod = max(1, (int) $progressMod);
86
87
        return $this;
88
    }
89
90
    /**
91
     * Set the total number of records prior to FLow execution
92
     *
93
     * @param int $numRecords
94
     *
95
     * @return $this
96
     */
97
    public function setNumRecords($numRecords)
98
    {
99
        $this->numRecords = $numRecords;
100
101
        return $this;
102
    }
103
104
    /**
105
     * Triggered when a Flow starts
106
     */
107
    public function start()
108
    {
109
        $this->getOutput()->writeln('<info>[YaEtl] Start</info>');
110
        $this->progressBar = new ProgressBar($this->output);
111
        $this->progressBar->start($this->numRecords);
112
    }
113
114
    /**
115
     * Triggered when a Flow progresses,
116
     * eg exec once or generates once
117
     */
118
    public function progress()
119
    {
120
        $this->progressBar->advance($this->progressMod);
121
    }
122
123
    /**
124
     * Triggered when a Flow succeeds
125
     *
126
     * @param FlowEventInterface $event
127
     */
128
    public function success(FlowEventInterface $event)
129
    {
130
        $this->progressBar->finish();
131
        $this->output->writeln(PHP_EOL);
132
        $flow       = $event->getFlow();
133
        $flowStatus = $flow->getFlowStatus();
134
        if ($flowStatus->isDirty()) {
135
            $this->output->writeln('<warn>[YaEtl] Dirty Success</warn>');
136
        } else {
137
            $this->output->writeln('<info>[YaEtl] Clean Success</info>');
138
        }
139
140
        $this->displayReport($flow);
141
    }
142
143
    /**
144
     * Triggered when a Flow fails
145
     *
146
     * @param FlowEventInterface $event
147
     */
148
    public function fail(FlowEventInterface $event)
149
    {
150
        $this->progressBar->finish();
151
        $this->output->writeln(PHP_EOL);
152
        $this->output->writeln('<error>[YaEtl] Failed</error>');
153
        $this->displayReport($event->getFlow());
154
    }
155
156
    /**
157
     * @return array
158
     */
159
    public static function getSubscribedEvents()
160
    {
161
        return [
162
            FlowEvent::FLOW_START    => ['start', 0],
163
            FlowEvent::FLOW_PROGRESS => ['progress', 0],
164
            FlowEvent::FLOW_SUCCESS  => ['success', 0],
165
            FlowEvent::FLOW_FAIL     => ['fail', 0],
166
        ];
167
    }
168
169
    /**
170
     * @param FlowInterface $flow
171
     *
172
     * @return $this
173
     */
174
    protected function displayReport(FlowInterface $flow)
175
    {
176
        $flowStats = $flow->getStats();
177
        $this->output->writeln($flowStats['report']);
178
179
        return $this;
180
    }
181
}
182