Completed
Push — master ( 3bd1cb...1e3022 )
by Christian
01:37
created

StatementRepository::storeStatement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
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 XApi\Repository\InMemory;
13
14
use Rhumsaa\Uuid\Uuid;
15
use Xabbuh\XApi\Common\Exception\NotFoundException;
16
use Xabbuh\XApi\Model\Actor;
17
use Xabbuh\XApi\Model\Statement;
18
use Xabbuh\XApi\Model\StatementId;
19
use Xabbuh\XApi\Model\StatementsFilter;
20
use XApi\Repository\Api\StatementRepositoryInterface;
21
22
/**
23
 * @author Christian Flothmann <[email protected]>
24
 */
25
final class StatementRepository implements StatementRepositoryInterface
26
{
27
    /**
28
     * @var Statement[]
29
     */
30
    private $statements = array();
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 24
    public function findStatementById(StatementId $statementId, Actor $authority = null)
36
    {
37 24
        if (!isset($this->statements[$statementId->getValue()])) {
38 1
            throw new NotFoundException(sprintf('A statement with id "%s" could not be found.', $statementId->getValue()));
39
        }
40
41 23
        $statement = $this->statements[$statementId->getValue()];
42
43 23
        if ($statement->isVoidStatement()) {
44 1
            throw new NotFoundException(sprintf('The statement with id "%s" is a voiding statement.', $statementId->getValue()));
45
        }
46
47 22
        return $statement;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 6
    public function findVoidedStatementById(StatementId $voidedStatementId, Actor $authority = null)
54
    {
55 6
        if (!isset($this->statements[$voidedStatementId->getValue()])) {
56 1
            throw new NotFoundException(sprintf('A statement with id "%s" could not be found.', $voidedStatementId->getValue()));
57
        }
58
59 5
        $statement = $this->statements[$voidedStatementId->getValue()];
60
61 5
        if (!$statement->isVoidStatement()) {
62 1
            throw new NotFoundException(sprintf('The statement with id "%s" is not a voiding statement.', $voidedStatementId->getValue()));
63
        }
64
65 4
        return $statement;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function findStatementsBy(StatementsFilter $criteria, Actor $authority = null)
72
    {
73
        return array();
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 54
    public function storeStatement(Statement $statement, $flush = true)
80
    {
81 54
        if (null === $statement->getId()) {
82 28
            $statement = $statement->withId(StatementId::fromUuid(Uuid::uuid4()));
83
        }
84
85 54
        $this->statements[$statement->getId()->getValue()] = $statement;
86
87 54
        return $statement->getId();
88
    }
89
}
90