Passed
Push — master ( b7f228...697126 )
by Fabrice
02:21
created

ProgressBarSubscriber::registerFlow()   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 fab2s\YaEtl\YaEtl;
16
use Symfony\Component\Console\Helper\ProgressBar;
17
use Symfony\Component\Console\Output\ConsoleOutput;
18
use Symfony\Component\Console\Output\ConsoleOutputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
22
/**
23
 * Class ProgressBarSubscriber
24
 */
25
class ProgressBarSubscriber implements EventSubscriberInterface
26
{
27
    /**
28
     * The Laravel output object, extracted from the command object
29
     *
30
     * @var ConsoleOutputInterface
31
     */
32
    protected $output;
33
34
    /**
35
     * The number of records
36
     *
37
     * @var int|null
38
     */
39
    protected $numRecords;
40
41
    /**
42
     * Progress modulo, should be aligned with YaEtl's one
43
     *
44
     * @var int
45
     */
46
    protected $progressMod = 1024;
47
48
    /**
49
     * @var ProgressBar
50
     */
51
    protected $progressBar;
52
53
    /**
54
     * ProgressBarSubscriber constructor.
55
     *
56
     * @param YaEtl|null $flow
57
     */
58
    public function __construct(YaEtl $flow = null)
59
    {
60
        if ($flow !== null) {
61
            // auto register
62
            $this->registerFlow($flow);
63
        }
64
    }
65
66
    /**
67
     * @param YaEtl $flow
68
     *
69
     * @return $this
70
     */
71
    public function registerFlow(YaEtl $flow)
72
    {
73
        $flow->getDispatcher()->addSubscriber($this);
74
75
        return $this;
76
    }
77
78
    /**
79
     * @return OutputInterface
80
     */
81
    public function getOutput()
82
    {
83
        if (!isset($this->output)) {
84
            $this->output = new ConsoleOutput;
85
        }
86
87
        return $this->output;
88
    }
89
90
    /**
91
     * @param OutputInterface $output
92
     *
93
     * @return ProgressBarSubscriber
94
     */
95
    public function setOutput(OutputInterface $output)
96
    {
97
        $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...
98
99
        return $this;
100
    }
101
102
    /**
103
     * Set progress modulo
104
     *
105
     * @param int $progressMod
106
     *
107
     * @return $this
108
     */
109
    public function setProgressMod($progressMod)
110
    {
111
        $this->progressMod = max(1, (int) $progressMod);
112
113
        return $this;
114
    }
115
116
    /**
117
     * Set the total number of records prior to FLow execution
118
     *
119
     * @param int $numRecords
120
     *
121
     * @return $this
122
     */
123
    public function setNumRecords($numRecords)
124
    {
125
        $this->numRecords = $numRecords;
126
127
        return $this;
128
    }
129
130
    /**
131
     * Triggered when a Flow starts
132
     *
133
     * @param FlowEventInterface $event
134
     */
135
    public function start(FlowEventInterface $event)
136
    {
137
        /** @var YaEtl $flow */
138
        $flow = $event->getFlow();
139
        $this->setProgressMod($flow->getProgressMod())
140
            ->getOutput()
141
            ->writeln('<info>[YaEtl] Start</info>');
142
        $this->progressBar = new ProgressBar($this->output);
143
        $this->progressBar->start($this->numRecords);
144
    }
145
146
    /**
147
     * Triggered when a Flow progresses,
148
     * eg exec once or generates once
149
     */
150
    public function progress()
151
    {
152
        $this->progressBar->advance($this->progressMod);
153
    }
154
155
    /**
156
     * Triggered when a Flow succeeds
157
     *
158
     * @param FlowEventInterface $event
159
     */
160
    public function success(FlowEventInterface $event)
161
    {
162
        $this->progressBar->finish();
163
        $this->output->writeln(PHP_EOL);
164
        $flow       = $event->getFlow();
165
        $flowStatus = $flow->getFlowStatus();
166
        if ($flowStatus->isDirty()) {
167
            $this->output->writeln('<warn>[YaEtl] Dirty Success</warn>');
168
        } else {
169
            $this->output->writeln('<info>[YaEtl] Clean Success</info>');
170
        }
171
172
        $this->displayReport($flow);
173
    }
174
175
    /**
176
     * Triggered when a Flow fails
177
     *
178
     * @param FlowEventInterface $event
179
     */
180
    public function fail(FlowEventInterface $event)
181
    {
182
        $this->progressBar->finish();
183
        $this->output->writeln(PHP_EOL);
184
        $this->output->writeln('<error>[YaEtl] Failed</error>');
185
        $this->displayReport($event->getFlow());
186
    }
187
188
    /**
189
     * @return array
190
     */
191
    public static function getSubscribedEvents()
192
    {
193
        return [
194
            FlowEvent::FLOW_START    => ['start', 0],
195
            FlowEvent::FLOW_PROGRESS => ['progress', 0],
196
            FlowEvent::FLOW_SUCCESS  => ['success', 0],
197
            FlowEvent::FLOW_FAIL     => ['fail', 0],
198
        ];
199
    }
200
201
    /**
202
     * @param FlowInterface $flow
203
     *
204
     * @return $this
205
     */
206
    protected function displayReport(FlowInterface $flow)
207
    {
208
        $flowStats = $flow->getStats();
209
        $this->output->writeln($flowStats['report']);
210
211
        return $this;
212
    }
213
}
214