Completed
Push — master ( ee41a7...105acf )
by Christian
11s
created

Statement::isVoidStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\XApi\Model;
13
14
/**
15
 * An Experience API {@link https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#statement Statement}.
16
 *
17
 * @author Christian Flothmann <[email protected]>
18
 */
19
final class Statement
20
{
21
    /**
22
     * @var StatementId|null The unique identifier
23
     */
24
    private $id;
25
26
    /**
27
     * @var Verb $verb The {@link Verb}
28
     */
29
    private $verb;
30
31
    /**
32
     * @var Actor The {@link Actor}
33
     */
34
    private $actor;
35
36
    /**
37
     * @var Object The {@link StatementObject}
38
     */
39
    private $object;
40
41
    /**
42
     * @var Result The {@link Activity} {@link Result}
43
     */
44
    private $result;
45
46
    /**
47
     * @var Actor The Authority that asserted the Statement true
48
     */
49
    private $authority;
50
51
    /**
52
     * @var \DateTime The timestamp of when the events described in this statement occurred
53
     */
54
    private $created;
55
56
    /**
57
     * @var \DateTime The timestamp of when this statement was recorded by the LRS
58
     */
59
    private $stored;
60
61 4
    /**
62
     * @var Context|null A context giving the statement more meaning
63 4
     */
64 4
    private $context;
65 4
66 4
    private $attachments;
67 4
68 4
    private $version;
69 4
70 4
    /**
71 4
     * @param StatementId|null  $id
72
     * @param Actor             $actor
73
     * @param Verb              $verb
74
     * @param StatementObject   $object
75
     * @param Result|null       $result
76
     * @param Actor|null        $authority
77
     * @param \DateTime|null    $created
78 2
     * @param \DateTime|null    $stored
79
     * @param Context|null      $context
80 2
     * @param Attachment[]|null $attachments
81
     * @param string|null       $version
82
     */
83
    public function __construct(StatementId $id = null, Actor $actor, Verb $verb, StatementObject $object, Result $result = null, Actor $authority = null, \DateTime $created = null, \DateTime $stored = null, Context $context = null, array $attachments = null, $version = null)
84
    {
85
        $this->id = $id;
86
        $this->actor = $actor;
87
        $this->verb = $verb;
88 3
        $this->object = $object;
89
        $this->result = $result;
90 3
        $this->authority = $authority;
91
        $this->created = $created;
92
        $this->stored = $stored;
93
        $this->context = $context;
94
        $this->attachments = null !== $attachments ? array_values($attachments) : null;
95
        $this->version = $version;
96
    }
97
98 3
    public function withId(StatementId $id = null)
99
    {
100 3
        $statement = clone $this;
101
        $statement->id = $id;
102
103
        return $statement;
104
    }
105
106
    public function withActor(Actor $actor)
107
    {
108 3
        $statement = clone $this;
109
        $statement->actor = $actor;
110 3
111
        return $statement;
112
    }
113
114
    public function withVerb(Verb $verb)
115
    {
116
        $statement = clone $this;
117
        $statement->verb = $verb;
118
119
        return $statement;
120
    }
121
122
    public function withObject(StatementObject $object)
123
    {
124
        $statement = clone $this;
125
        $statement->object = $object;
126
127
        return $statement;
128 2
    }
129
130 2
    public function withResult(Result $result = null)
131
    {
132
        $statement = clone $this;
133
        $statement->result = $result;
134
135
        return $statement;
136
    }
137
138
    /**
139
     * Creates a new Statement based on the current one containing an Authority
140
     * that asserts the Statement true.
141
     *
142
     * @param Actor $authority The Authority asserting the Statement true
143
     *
144
     * @return Statement The new Statement
145
     */
146
    public function withAuthority(Actor $authority = null)
147
    {
148
        $statement = clone $this;
149
        $statement->authority = $authority;
150
151
        return $statement;
152
    }
153
154
    public function withCreated(\DateTime $created = null)
155
    {
156
        $statement = clone $this;
157
        $statement->created = $created;
158
159
        return $statement;
160
    }
161
162
    public function withStored(\DateTime $stored = null)
163
    {
164
        $statement = clone $this;
165
        $statement->stored = $stored;
166
167
        return $statement;
168
    }
169
170 2
    public function withContext(Context $context = null)
171
    {
172 2
        $statement = clone $this;
173
        $statement->context = $context;
174 2
175
        return $statement;
176
    }
177
178
    /**
179
     * @param Attachment[]|null $attachments
180
     *
181
     * @return self
182
     */
183
    public function withAttachments(array $attachments = null)
184 1
    {
185
        $statement = clone $this;
186 1
        $statement->attachments = null !== $attachments ? array_values($attachments) : null;
187 1
188
        return $statement;
189 1
    }
190 1
191
    /**
192
     * @param string $version
193
     *
194
     * @return self
195
     */
196
    public function withVersion($version)
197
    {
198
        $statement = clone $this;
199
        $statement->version = $version;
200
201
        return $statement;
202 2
    }
203
204 2
    /**
205
     * Returns the Statement's unique identifier.
206
     *
207
     * @return StatementId|null The identifier
208
     */
209
    public function getId()
210
    {
211
        return $this->id;
212
    }
213
214
    /**
215
     * Returns the Statement's {@link Verb}.
216 2
     *
217
     * @return Verb The Verb
218 2
     */
219
    public function getVerb()
220
    {
221
        return $this->verb;
222 2
    }
223
224
    /**
225
     * Returns the Statement's {@link Actor}.
226 2
     *
227
     * @return Actor The Actor
228
     */
229
    public function getActor()
230 2
    {
231
        return $this->actor;
232
    }
233
234 2
    /**
235
     * Returns the Statement's {@link StatementObject}.
236
     *
237
     * @return \Xabbuh\XApi\Model\Object The Object
238 2
     */
239
    public function getObject()
240
    {
241
        return $this->object;
242 2
    }
243
244
    /**
245
     * Returns the {@link Activity} {@link Result}.
246 2
     *
247 1
     * @return Result The Result
248
     */
249
    public function getResult()
250 1
    {
251
        return $this->result;
252
    }
253
254 1
    /**
255 1
     * Returns the Authority that asserted the Statement true.
256
     *
257
     * @return Actor The Authority
258
     */
259
    public function getAuthority()
260
    {
261
        return $this->authority;
262
    }
263
264
    /**
265
     * Returns the timestamp of when the events described in this statement
266
     * occurred.
267
     *
268
     * @return \DateTime The timestamp
269
     */
270
    public function getCreated()
271
    {
272
        return $this->created;
273
    }
274
275
    /**
276
     * Returns the timestamp of when this statement was recorded by the LRS.
277
     *
278
     * @return \DateTime The timestamp
279
     */
280
    public function getStored()
281
    {
282
        return $this->stored;
283
    }
284
285
    /**
286
     * Returns the context that gives the statement more meaning.
287
     *
288
     * @return Context|null
289
     */
290
    public function getContext()
291
    {
292
        return $this->context;
293
    }
294
295
    public function getAttachments()
296
    {
297
        return $this->attachments;
298
    }
299
300
    public function getVersion()
301
    {
302
        return $this->version;
303
    }
304
305
    /**
306
     * Tests whether or not this Statement is a void Statement (i.e. it voids
307
     * another Statement).
308
     *
309
     * @return bool True if the Statement voids another Statement, false otherwise
310
     */
311
    public function isVoidStatement()
312
    {
313
        return $this->verb->isVoidVerb();
314
    }
315
316
    /**
317
     * Returns a {@link StatementReference} for the Statement.
318
     *
319
     * @return StatementReference The reference
320
     */
321
    public function getStatementReference()
322
    {
323
        $reference = new StatementReference($this->id);
0 ignored issues
show
Bug introduced by
It seems like $this->id can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
324
325
        return $reference;
326
    }
327
328
    /**
329
     * Returns a Statement that voids the current Statement.
330
     *
331
     * @param Actor $actor The Actor voiding this Statement
332
     *
333
     * @return Statement The voiding Statement
334
     */
335
    public function getVoidStatement(Actor $actor)
336
    {
337
        return new Statement(
338
            null,
339
            $actor,
340
            Verb::createVoidVerb(),
341
            $this->getStatementReference()
342
        );
343
    }
344
345
    /**
346
     * Checks if another statement is equal.
347
     *
348
     * Two statements are equal if and only if all of their properties are equal.
349
     *
350
     * @param Statement $statement The statement to compare with
351
     *
352
     * @return bool True if the statements are equal, false otherwise
353
     */
354
    public function equals(Statement $statement)
355
    {
356
        if (null !== $this->id xor null !== $statement->id) {
357
            return false;
358
        }
359
360
        if (null !== $this->id && null !== $statement->id && !$this->id->equals($statement->id)) {
361
            return false;
362
        }
363
364
        if (!$this->actor->equals($statement->actor)) {
365
            return false;
366
        }
367
368
        if (!$this->verb->equals($statement->verb)) {
369
            return false;
370
        }
371
372
        if (!$this->object->equals($statement->object)) {
373
            return false;
374
        }
375
376
        if (null === $this->result && null !== $statement->result) {
377
            return false;
378
        }
379
380
        if (null !== $this->result && null === $statement->result) {
381
            return false;
382
        }
383
384
        if (null !== $this->result && !$this->result->equals($statement->result)) {
385
            return false;
386
        }
387
388
        if (null === $this->authority && null !== $statement->authority) {
389
            return false;
390
        }
391
392
        if (null !== $this->authority && null === $statement->authority) {
393
            return false;
394
        }
395
396
        if (null !== $this->authority && !$this->authority->equals($statement->authority)) {
397
            return false;
398
        }
399
400
        if ($this->created != $statement->created) {
401
            return false;
402
        }
403
404
        if (null !== $this->context xor null !== $statement->context) {
405
            return false;
406
        }
407
408
        if (null !== $this->context && null !== $statement->context && !$this->context->equals($statement->context)) {
409
            return false;
410
        }
411
412
        if (null !== $this->attachments xor null !== $statement->attachments) {
413
            return false;
414
        }
415
416
        if (null !== $this->attachments && null !== $statement->attachments) {
417
            if (count($this->attachments) !== count($statement->attachments)) {
418
                return false;
419
            }
420
421
            foreach ($this->attachments as $key => $attachment) {
422
                if (!$attachment->equals($statement->attachments[$key])) {
423
                    return false;
424
                }
425
            }
426
        }
427
428
        return true;
429
    }
430
}
431