Passed
Push — etls ( 3c161b )
by Fabrice
11:34
created

EtlAbstract::run()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 15
rs 10
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()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getLimit() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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