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\Etl; |
11
|
|
|
|
12
|
|
|
use DateTimeImmutable; |
13
|
|
|
use DateTimeInterface; |
14
|
|
|
use Exception; |
15
|
|
|
use fab2s\NodalFlow\NodalFlowException; |
16
|
|
|
use fab2s\YaEtl\Events\ProgressBarSubscriber; |
17
|
|
|
use fab2s\YaEtl\YaEtl; |
18
|
|
|
use fab2s\YaEtl\YaEtlException; |
19
|
|
|
use ReflectionException; |
20
|
|
|
|
21
|
|
|
abstract class EtlAbstract |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var int|null |
25
|
|
|
*/ |
26
|
|
|
protected $limit = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The immutable time starting from when getNow or setNow are called |
30
|
|
|
* |
31
|
|
|
* @var DateTimeImmutable |
32
|
|
|
*/ |
33
|
|
|
protected $now; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var YaEtl |
37
|
|
|
*/ |
38
|
|
|
protected $etl; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
protected $activateProgressBar = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var mixed|null |
47
|
|
|
*/ |
48
|
|
|
protected $etlResult; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @throws NodalFlowException |
52
|
|
|
* @throws YaEtlException|ReflectionException |
53
|
|
|
* |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function run(): self |
57
|
|
|
{ |
58
|
|
|
$this->etlResult = null; |
59
|
|
|
$etl = $this->getEtl(); |
60
|
|
|
if ($this->activateProgressBar) { |
61
|
|
|
new ProgressBarSubscriber($etl); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($this->getLimit()) { |
|
|
|
|
65
|
|
|
$etl->limit($this->getLimit()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->etlResult = $etl->exec(); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getEtlResult() |
74
|
|
|
{ |
75
|
|
|
return $this->etlResult; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function activateProgressBar(bool $activateProgressBar = true): self |
79
|
|
|
{ |
80
|
|
|
$this->activateProgressBar = $activateProgressBar; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getLimit(): ?int |
86
|
|
|
{ |
87
|
|
|
return $this->limit; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function setLimit(?int $limit): self |
91
|
|
|
{ |
92
|
|
|
$this->limit = $limit; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @throws Exception |
99
|
|
|
*/ |
100
|
|
|
public function getNow(): DateTimeImmutable |
101
|
|
|
{ |
102
|
|
|
if (isset($this->now)) { |
103
|
|
|
return $this->now; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->now = new DateTimeImmutable('@' . time()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function setNow(DateTimeInterface $now): self |
110
|
|
|
{ |
111
|
|
|
$this->now = $now instanceof DateTimeImmutable ? $now : DateTimeImmutable::createFromMutable($now); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @throws NodalFlowException |
118
|
|
|
* @throws YaEtlException |
119
|
|
|
*/ |
120
|
|
|
abstract protected function getEtl(): YaEtl; |
121
|
|
|
} |
122
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: