Completed
Pull Request — master (#32)
by Christian
02:13
created

Statement::getAttachments()   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 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
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 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 = $attachments;
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)
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)
143
    {
144
        $statement = clone $this;
145
        $statement->authority = $authority;
146
147
        return $statement;
148
    }
149
150
    public function withTimestamp(\DateTime $timestamp)
151
    {
152
        $statement = clone $this;
153
        $statement->created = $timestamp;
154
155
        return $statement;
156
    }
157
158
    public function withStored(\DateTime $stored)
159
    {
160
        $statement = clone $this;
161
        $statement->stored = $stored;
162
163
        return $statement;
164
    }
165
166
    public function withContext(Context $context)
167
    {
168
        $statement = clone $this;
169
        $statement->context = $context;
170 2
171
        return $statement;
172 2
    }
173
174 2
    /**
175
     * Returns the Statement's unique identifier.
176
     *
177
     * @return StatementId|null The identifier
178
     */
179
    public function getId()
180
    {
181
        return $this->id;
182
    }
183
184 1
    /**
185
     * Returns the Statement's {@link Verb}.
186 1
     *
187 1
     * @return Verb The Verb
188
     */
189 1
    public function getVerb()
190 1
    {
191
        return $this->verb;
192
    }
193
194
    /**
195
     * Returns the Statement's {@link Actor}.
196
     *
197
     * @return Actor The Actor
198
     */
199
    public function getActor()
200
    {
201
        return $this->actor;
202 2
    }
203
204 2
    /**
205
     * Returns the Statement's {@link Object}.
206
     *
207
     * @return \Xabbuh\XApi\Model\Object The Object
208
     */
209
    public function getObject()
210
    {
211
        return $this->object;
212
    }
213
214
    /**
215
     * Returns the {@link Activity} {@link Result}.
216 2
     *
217
     * @return Result The Result
218 2
     */
219
    public function getResult()
220
    {
221
        return $this->result;
222 2
    }
223
224
    /**
225
     * Returns the Authority that asserted the Statement true.
226 2
     *
227
     * @return Actor The Authority
228
     */
229
    public function getAuthority()
230 2
    {
231
        return $this->authority;
232
    }
233
234 2
    /**
235
     * Returns the timestamp of when the events described in this statement
236
     * occurred.
237
     *
238 2
     * @return \DateTime The timestamp
239
     */
240
    public function getTimestamp()
241
    {
242 2
        return $this->created;
243
    }
244
245
    /**
246 2
     * Returns the timestamp of when the events described in this statement
247 1
     * occurred.
248
     *
249
     * @return \DateTime The timestamp
250 1
     */
251
    public function getCreated()
252
    {
253
        return $this->created;
254 1
    }
255 1
256
    /**
257
     * Returns the timestamp of when this statement was recorded by the LRS.
258
     *
259
     * @return \DateTime The timestamp
260
     */
261
    public function getStored()
262
    {
263
        return $this->stored;
264
    }
265
266
    /**
267
     * Returns the context that gives the statement more meaning.
268
     *
269
     * @return Context|null
270
     */
271
    public function getContext()
272
    {
273
        return $this->context;
274
    }
275
276
    public function getAttachments()
277
    {
278
        return $this->attachments;
279
    }
280
281
    /**
282
     * Tests whether or not this Statement is a void Statement (i.e. it voids
283
     * another Statement).
284
     *
285
     * @return bool True if the Statement voids another Statement, false otherwise
286
     */
287
    public function isVoidStatement()
288
    {
289
        return $this->verb->isVoidVerb();
290
    }
291
292
    /**
293
     * Returns a {@link StatementReference} for the Statement.
294
     *
295
     * @return StatementReference The reference
296
     */
297
    public function getStatementReference()
298
    {
299
        $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...
300
301
        return $reference;
302
    }
303
304
    /**
305
     * Returns a Statement that voids the current Statement.
306
     *
307
     * @param Actor $actor The Actor voiding this Statement
308
     *
309
     * @return Statement The voiding Statement
310
     */
311
    public function getVoidStatement(Actor $actor)
312
    {
313
        return new Statement(
314
            null,
315
            $actor,
316
            Verb::createVoidVerb(),
317
            $this->getStatementReference()
318
        );
319
    }
320
321
    /**
322
     * Checks if another statement is equal.
323
     *
324
     * Two statements are equal if and only if all of their properties are equal.
325
     *
326
     * @param Statement $statement The statement to compare with
327
     *
328
     * @return bool True if the statements are equal, false otherwise
329
     */
330
    public function equals(Statement $statement)
331
    {
332
        if (null === $this->id && null !== $statement->id) {
333
            return false;
334
        }
335
336
        if (null !== $this->id && !$this->id->equals($statement->id)) {
0 ignored issues
show
Bug introduced by
It seems like $statement->id can be null; however, equals() 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...
337
            return false;
338
        }
339
340
        if (!$this->actor->equals($statement->actor)) {
341
            return false;
342
        }
343
344
        if (!$this->verb->equals($statement->verb)) {
345
            return false;
346
        }
347
348
        if (!$this->object->equals($statement->object)) {
349
            return false;
350
        }
351
352
        if (null === $this->result && null !== $statement->result) {
353
            return false;
354
        }
355
356
        if (null !== $this->result && null === $statement->result) {
357
            return false;
358
        }
359
360
        if (null !== $this->result && !$this->result->equals($statement->result)) {
361
            return false;
362
        }
363
364
        if (null === $this->authority && null !== $statement->authority) {
365
            return false;
366
        }
367
368
        if (null !== $this->authority && null === $statement->authority) {
369
            return false;
370
        }
371
372
        if (null !== $this->authority && !$this->authority->equals($statement->authority)) {
373
            return false;
374
        }
375
376
        if ($this->created != $statement->created) {
377
            return false;
378
        }
379
380
        return true;
381
    }
382
}
383