Completed
Push — master ( 11b0cf...778938 )
by Markus
05:18
created

ImportRun   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 93.24%

Importance

Changes 18
Bugs 2 Features 2
Metric Value
wmc 34
c 18
b 2
f 2
lcom 2
cbo 1
dl 0
loc 232
ccs 69
cts 74
cp 0.9324
rs 9.2

22 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A getContext() 0 4 1
A getId() 0 4 1
A getCreatedBy() 0 4 1
A __construct() 0 7 1
A setContext() 0 11 3
A revoke() 0 11 2
A reset() 0 6 1
A isRevoked() 0 4 1
A finish() 0 6 1
A isFinished() 0 4 1
A validated() 0 7 2
A getValidationMessages() 0 4 1
A isValidated() 0 4 2
A isRunnable() 0 4 2
A getConfiguration() 0 4 1
A setStatistics() 0 6 1
A getStatistics() 0 4 1
A setInfo() 0 6 1
A getInfo() 0 4 1
A getState() 0 14 4
A toArray() 0 14 4
1
<?php
2
namespace Mathielen\ImportEngine\ValueObject;
3
4
class ImportRun
5
{
6
7
    const STATE_REVOKED = 'revoked';
8
    const STATE_FINISHED = 'finished';
9
    const STATE_CREATED = 'created';
10
    const STATE_VALIDATED = 'validated';
11
12
    protected $id;
13
14
    /**
15
     * @var ImportConfiguration
16
     */
17
    protected $configuration;
18
19
    /**
20
     * @var \DateTime
21
     */
22
    protected $createdAt;
23
    protected $createdBy;
24
25
    /**
26
     * @var \DateTime
27
     */
28
    protected $validatedAt;
29
    protected $validationMessages;
30
31
    /**
32
     * @var \DateTime
33
     */
34
    protected $finishedAt;
35
36
    /**
37
     * @var \DateTime
38
     */
39
    protected $revokedAt;
40
    protected $revokedBy;
41
42
    protected $statistics = array();
43
    protected $info = array();
44
45
    /**
46
     * arbitrary data
47
     */
48
    protected $context;
49
50 18
    public function __construct(ImportConfiguration $configuration=null, $createdBy = null)
51
    {
52 18
        $this->id = uniqid();
53 18
        $this->createdAt = new \DateTime();
54 18
        $this->configuration = $configuration;
55 18
        $this->createdBy = $createdBy;
56 18
    }
57
58
    /**
59
     * @return ImportRun
60
     */
61
    public static function create(ImportConfiguration $configuration=null, $createdBy = null)
62
    {
63
        return new self($configuration, $createdBy);
64
    }
65
66
    /**
67
     * @return ImportRun
68
     */
69 1
    public function setContext($context)
70
    {
71
        //merge existing context with new one, if both are arrays
72 1
        if (is_array($context) && is_array($this->context)) {
73
            $context = array_merge($this->context, $context);
74
        }
75
76 1
        $this->context = $context;
77
78 1
        return $this;
79
    }
80
81 6
    public function getContext()
82
    {
83 6
        return $this->context;
84
    }
85
86 2
    public function getId()
87
    {
88 2
        return $this->id;
89
    }
90
91 1
    public function getCreatedBy()
92
    {
93 1
        return $this->createdBy;
94
    }
95
96
    /**
97
     * @return ImportRun
98
     */
99 1
    public function revoke($revokedBy = null)
100
    {
101 1
        if (!$this->isFinished()) {
102
            throw new \LogicException('Cannot revoke import if not already finished.');
103
        }
104
105 1
        $this->revokedAt = new \DateTime();
106 1
        $this->revokedBy = $revokedBy;
107
108 1
        return $this;
109
    }
110
111 3
    /**
112
     * @return ImportRun
113 3
     */
114
    public function reset()
115
    {
116
        $this->finishedAt = null;
117
118
        return $this;
119 10
    }
120
121 10
    public function isRevoked()
122
    {
123 10
        return $this->getState() == self::STATE_REVOKED;
124
    }
125
126 4
    /**
127
     * @return ImportRun
128 4
     */
129
    public function finish()
130
    {
131
        $this->finishedAt = new \DateTime();
132
133
        return $this;
134 1
    }
135
136 1
    public function isFinished()
137 1
    {
138
        return $this->getState() == self::STATE_FINISHED;
139 1
    }
140
141
    /**
142 1
     * @return ImportRun
143
     */
144 1
    public function validated(array $validationMessages=null)
145
    {
146
        $this->validatedAt = empty($this->validatedAt)?new \DateTime():$this->validatedAt;
147 1
        $this->validationMessages = $validationMessages;
148
149 1
        return $this;
150
    }
151
152 4
    public function getValidationMessages()
153
    {
154 4
        return $this->validationMessages;
155
    }
156
157
    public function isValidated()
158
    {
159
        return $this->isFinished() || $this->getState() == self::STATE_VALIDATED;
160 1
    }
161
162 1
    public function isRunnable()
163
    {
164
        return !$this->isFinished() && !$this->isRevoked();
165
    }
166
167
    /**
168 10
     * @return ImportConfiguration
169
     */
170 10
    public function getConfiguration()
171
    {
172 10
        return $this->configuration;
173
    }
174
175 1
    /**
176
     * @return ImportRun
177 1
     */
178
    public function setStatistics(array $statistics)
179
    {
180
        $this->statistics = $statistics;
181
182
        return $this;
183 2
    }
184
185 2
    public function getStatistics()
186
    {
187 2
        return $this->statistics;
188
    }
189
190 1
    /**
191
     * @return ImportRun
192 1
     */
193
    public function setInfo(array $info)
194
    {
195 6
        $this->info = $info;
196
197 6
        return $this;
198 1
    }
199
200 6
    public function getInfo()
201 3
    {
202
        return $this->info;
203 4
    }
204 1
205
    public function getState()
206
    {
207 4
        if (!empty($this->revokedAt)) {
208
            return self::STATE_REVOKED;
209
        }
210 2
        if (!empty($this->finishedAt)) {
211
            return self::STATE_FINISHED;
212
        }
213 2
        if (!empty($this->validatedAt)) {
214 2
            return self::STATE_VALIDATED;
215 2
        }
216 2
217 2
        return self::STATE_CREATED;
218 2
    }
219 2
220 2
    public function toArray()
221 2
    {
222 2
        return array(
223
            'id' => $this->id,
224
            'configuration' => $this->configuration?$this->configuration->toArray():null,
225
            'statistics' => $this->statistics,
226
            'created_by' => $this->createdBy,
227
            'created_at' => $this->createdAt->getTimestamp(),
228
            'revoked_by' => $this->revokedBy,
229
            'revoked_at' => $this->revokedAt?$this->revokedAt->getTimestamp():null,
230
            'finished_at' => $this->finishedAt?$this->finishedAt->getTimestamp():null,
231
            'state' => $this->getState()
232
        );
233
    }
234
235
}
236