Completed
Push — master ( 778938...5ff82c )
by Markus
11:26
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.51%

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 72
cts 77
cp 0.9351
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 isRevoked() 0 4 1
A getValidationMessages() 0 4 1
A getConfiguration() 0 4 1
A getStatistics() 0 4 1
A setInfo() 0 6 1
A getState() 0 14 4
A reset() 0 6 1
A finish() 0 6 1
A isFinished() 0 4 1
A validated() 0 7 2
A isValidated() 0 4 2
A isRunnable() 0 4 2
A setStatistics() 0 6 1
A getInfo() 0 4 1
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 19
    public function __construct(ImportConfiguration $configuration=null, $createdBy = null)
51
    {
52 19
        $this->id = uniqid();
53 19
        $this->createdAt = new \DateTime();
54 19
        $this->configuration = $configuration;
55 19
        $this->createdBy = $createdBy;
56 19
    }
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
    /**
112
     * @return ImportRun
113
     */
114 1
    public function reset()
115
    {
116 1
        $this->finishedAt = null;
117
118 1
        return $this;
119
    }
120
121 4
    public function isRevoked()
122
    {
123 4
        return $this->getState() == self::STATE_REVOKED;
124
    }
125
126
    /**
127
     * @return ImportRun
128
     */
129 11
    public function finish()
130
    {
131 11
        $this->finishedAt = new \DateTime();
132
133 11
        return $this;
134
    }
135
136 5
    public function isFinished()
137
    {
138 5
        return $this->getState() == self::STATE_FINISHED;
139
    }
140
141
    /**
142
     * @return ImportRun
143
     */
144 1
    public function validated(array $validationMessages=null)
145
    {
146 1
        $this->validatedAt = empty($this->validatedAt)?new \DateTime():$this->validatedAt;
147 1
        $this->validationMessages = $validationMessages;
148
149 1
        return $this;
150
    }
151
152 1
    public function getValidationMessages()
153
    {
154 1
        return $this->validationMessages;
155
    }
156
157 1
    public function isValidated()
158
    {
159 1
        return $this->isFinished() || $this->getState() == self::STATE_VALIDATED;
160
    }
161
162 5
    public function isRunnable()
163
    {
164 5
        return !$this->isFinished() && !$this->isRevoked();
165
    }
166
167
    /**
168
     * @return ImportConfiguration
169
     */
170 1
    public function getConfiguration()
171
    {
172 1
        return $this->configuration;
173
    }
174
175
    /**
176
     * @return ImportRun
177
     */
178 10
    public function setStatistics(array $statistics)
179
    {
180 10
        $this->statistics = $statistics;
181
182 10
        return $this;
183
    }
184
185 1
    public function getStatistics()
186
    {
187 1
        return $this->statistics;
188
    }
189
190
    /**
191
     * @return ImportRun
192
     */
193 2
    public function setInfo(array $info)
194
    {
195 2
        $this->info = $info;
196
197 2
        return $this;
198
    }
199
200 1
    public function getInfo()
201
    {
202 1
        return $this->info;
203
    }
204
205 7
    public function getState()
206
    {
207 7
        if (!empty($this->revokedAt)) {
208 1
            return self::STATE_REVOKED;
209
        }
210 7
        if (!empty($this->finishedAt)) {
211 3
            return self::STATE_FINISHED;
212
        }
213 5
        if (!empty($this->validatedAt)) {
214 1
            return self::STATE_VALIDATED;
215
        }
216
217 5
        return self::STATE_CREATED;
218
    }
219
220 2
    public function toArray()
221
    {
222
        return array(
223 2
            'id' => $this->id,
224 2
            'configuration' => $this->configuration?$this->configuration->toArray():null,
225 2
            'statistics' => $this->statistics,
226 2
            'created_by' => $this->createdBy,
227 2
            'created_at' => $this->createdAt->getTimestamp(),
228 2
            'revoked_by' => $this->revokedBy,
229 2
            'revoked_at' => $this->revokedAt?$this->revokedAt->getTimestamp():null,
230 2
            'finished_at' => $this->finishedAt?$this->finishedAt->getTimestamp():null,
231 2
            'state' => $this->getState()
232 2
        );
233
    }
234
235
}
236