Completed
Push — master ( 72afff...030216 )
by Christian
02:10
created

Statement::withId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 4
nc 1
nop 1
crap 1
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 Object}
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
    /**
69 4
     * @param StatementId|null  $id
70 4
     * @param Actor             $actor
71 4
     * @param Verb              $verb
72
     * @param Object            $object
73
     * @param Result|null       $result
74
     * @param Actor|null        $authority
75
     * @param \DateTime|null    $created
76
     * @param \DateTime|null    $stored
77
     * @param Context|null      $context
78 2
     * @param Attachment[]|null $attachments
79
     */
80 2
    public function __construct(StatementId $id = null, Actor $actor, Verb $verb, Object $object, Result $result = null, Actor $authority = null, \DateTime $created = null, \DateTime $stored = null, Context $context = null, array $attachments = null)
81
    {
82
        $this->id = $id;
83
        $this->actor = $actor;
84
        $this->verb = $verb;
85
        $this->object = $object;
86
        $this->result = $result;
87
        $this->authority = $authority;
88 3
        $this->created = $created;
89
        $this->stored = $stored;
90 3
        $this->context = $context;
91
        $this->attachments = null !== $attachments ? array_values($attachments) : null;
92
    }
93
94
    public function withId(StatementId $id = null)
95
    {
96
        $statement = clone $this;
97
        $statement->id = $id;
98 3
99
        return $statement;
100 3
    }
101
102
    public function withActor(Actor $actor)
103
    {
104
        $statement = clone $this;
105
        $statement->actor = $actor;
106
107
        return $statement;
108 3
    }
109
110 3
    public function withVerb(Verb $verb)
111
    {
112
        $statement = clone $this;
113
        $statement->verb = $verb;
114
115
        return $statement;
116
    }
117
118
    public function withObject(Object $object)
119
    {
120
        $statement = clone $this;
121
        $statement->object = $object;
122
123
        return $statement;
124
    }
125
126
    public function withResult(Result $result = null)
127
    {
128 2
        $statement = clone $this;
129
        $statement->result = $result;
130 2
131
        return $statement;
132
    }
133
134
    /**
135
     * Creates a new Statement based on the current one containing an Authority
136
     * that asserts the Statement true.
137
     *
138
     * @param Actor $authority The Authority asserting the Statement true
139
     *
140
     * @return Statement The new Statement
141
     */
142
    public function withAuthority(Actor $authority = null)
143
    {
144
        $statement = clone $this;
145
        $statement->authority = $authority;
146
147
        return $statement;
148
    }
149
150
    public function withTimestamp(\DateTime $timestamp = null)
151
    {
152
        $statement = clone $this;
153
        $statement->created = $timestamp;
154
155
        return $statement;
156
    }
157
158
    public function withStored(\DateTime $stored = null)
159
    {
160
        $statement = clone $this;
161
        $statement->stored = $stored;
162
163
        return $statement;
164
    }
165
166
    public function withContext(Context $context = null)
167
    {
168
        $statement = clone $this;
169
        $statement->context = $context;
170 2
171
        return $statement;
172 2
    }
173
174 2
    /**
175
     * @param Attachment[]|null $attachments
176
     *
177
     * @return self
178
     */
179
    public function withAttachments(array $attachments = null)
180
    {
181
        $statement = clone $this;
182
        $statement->attachments = null !== $attachments ? array_values($attachments) : null;
183
184 1
        return $statement;
185
    }
186 1
187 1
    /**
188
     * Returns the Statement's unique identifier.
189 1
     *
190 1
     * @return StatementId|null The identifier
191
     */
192
    public function getId()
193
    {
194
        return $this->id;
195
    }
196
197
    /**
198
     * Returns the Statement's {@link Verb}.
199
     *
200
     * @return Verb The Verb
201
     */
202 2
    public function getVerb()
203
    {
204 2
        return $this->verb;
205
    }
206
207
    /**
208
     * Returns the Statement's {@link Actor}.
209
     *
210
     * @return Actor The Actor
211
     */
212
    public function getActor()
213
    {
214
        return $this->actor;
215
    }
216 2
217
    /**
218 2
     * Returns the Statement's {@link Object}.
219
     *
220
     * @return \Xabbuh\XApi\Model\Object The Object
221
     */
222 2
    public function getObject()
223
    {
224
        return $this->object;
225
    }
226 2
227
    /**
228
     * Returns the {@link Activity} {@link Result}.
229
     *
230 2
     * @return Result The Result
231
     */
232
    public function getResult()
233
    {
234 2
        return $this->result;
235
    }
236
237
    /**
238 2
     * Returns the Authority that asserted the Statement true.
239
     *
240
     * @return Actor The Authority
241
     */
242 2
    public function getAuthority()
243
    {
244
        return $this->authority;
245
    }
246 2
247 1
    /**
248
     * Returns the timestamp of when the events described in this statement
249
     * occurred.
250 1
     *
251
     * @return \DateTime The timestamp
252
     */
253
    public function getTimestamp()
254 1
    {
255 1
        return $this->created;
256
    }
257
258
    /**
259
     * Returns the timestamp of when the events described in this statement
260
     * occurred.
261
     *
262
     * @return \DateTime The timestamp
263
     */
264
    public function getCreated()
265
    {
266
        return $this->created;
267
    }
268
269
    /**
270
     * Returns the timestamp of when this statement was recorded by the LRS.
271
     *
272
     * @return \DateTime The timestamp
273
     */
274
    public function getStored()
275
    {
276
        return $this->stored;
277
    }
278
279
    /**
280
     * Returns the context that gives the statement more meaning.
281
     *
282
     * @return Context|null
283
     */
284
    public function getContext()
285
    {
286
        return $this->context;
287
    }
288
289
    public function getAttachments()
290
    {
291
        return $this->attachments;
292
    }
293
294
    /**
295
     * Tests whether or not this Statement is a void Statement (i.e. it voids
296
     * another Statement).
297
     *
298
     * @return bool True if the Statement voids another Statement, false otherwise
299
     */
300
    public function isVoidStatement()
301
    {
302
        return $this->verb->isVoidVerb();
303
    }
304
305
    /**
306
     * Returns a {@link StatementReference} for the Statement.
307
     *
308
     * @return StatementReference The reference
309
     */
310
    public function getStatementReference()
311
    {
312
        $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...
313
314
        return $reference;
315
    }
316
317
    /**
318
     * Returns a Statement that voids the current Statement.
319
     *
320
     * @param Actor $actor The Actor voiding this Statement
321
     *
322
     * @return Statement The voiding Statement
323
     */
324
    public function getVoidStatement(Actor $actor)
325
    {
326
        return new Statement(
327
            null,
328
            $actor,
329
            Verb::createVoidVerb(),
330
            $this->getStatementReference()
331
        );
332
    }
333
334
    /**
335
     * Checks if another statement is equal.
336
     *
337
     * Two statements are equal if and only if all of their properties are equal.
338
     *
339
     * @param Statement $statement The statement to compare with
340
     *
341
     * @return bool True if the statements are equal, false otherwise
342
     */
343
    public function equals(Statement $statement)
344
    {
345
        if (null !== $this->id xor null !== $statement->id) {
346
            return false;
347
        }
348
349
        if (null !== $this->id && null !== $statement->id && !$this->id->equals($statement->id)) {
350
            return false;
351
        }
352
353
        if (!$this->actor->equals($statement->actor)) {
354
            return false;
355
        }
356
357
        if (!$this->verb->equals($statement->verb)) {
358
            return false;
359
        }
360
361
        if (!$this->object->equals($statement->object)) {
362
            return false;
363
        }
364
365
        if (null === $this->result && null !== $statement->result) {
366
            return false;
367
        }
368
369
        if (null !== $this->result && null === $statement->result) {
370
            return false;
371
        }
372
373
        if (null !== $this->result && !$this->result->equals($statement->result)) {
374
            return false;
375
        }
376
377
        if (null === $this->authority && null !== $statement->authority) {
378
            return false;
379
        }
380
381
        if (null !== $this->authority && null === $statement->authority) {
382
            return false;
383
        }
384
385
        if (null !== $this->authority && !$this->authority->equals($statement->authority)) {
386
            return false;
387
        }
388
389
        if ($this->created != $statement->created) {
390
            return false;
391
        }
392
393
        if (null !== $this->context xor null !== $statement->context) {
394
            return false;
395
        }
396
397
        if (null !== $this->context && null !== $statement->context && !$this->context->equals($statement->context)) {
398
            return false;
399
        }
400
401
        if (null !== $this->attachments xor null !== $statement->attachments) {
402
            return false;
403
        }
404
405
        if (null !== $this->attachments && null !== $statement->attachments) {
406
            if (count($this->attachments) !== count($statement->attachments)) {
407
                return false;
408
            }
409
410
            foreach ($this->attachments as $key => $attachment) {
411
                if (!$attachment->equals($statement->attachments[$key])) {
412
                    return false;
413
                }
414
            }
415
        }
416
417
        return true;
418
    }
419
}
420