Completed
Push — master ( c233b7...aa3611 )
by Christian
02:29
created

StatementRepository::findVoidedStatementById()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 11

Duplication

Lines 22
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 22
loc 22
rs 8.9197
cc 4
eloc 11
nc 6
nop 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\Api;
13
14
use Rhumsaa\Uuid\Uuid;
15
use Xabbuh\XApi\Model\Actor;
16
use Xabbuh\XApi\Model\Statement;
17
use Xabbuh\XApi\Model\StatementsFilter;
18
use XApi\Repository\Api\Exception\NotFoundException;
19
use XApi\Repository\Api\Mapping\MappedStatement;
20
21
/**
22
 * {@link Statement} repository.
23
 *
24
 * @author Christian Flothmann <[email protected]>
25
 */
26
abstract class StatementRepository
27
{
28
    /**
29
     * Finds a {@link Statement} by id.
30
     *
31
     * @param string     $statementId The statement id to filter by
32
     * @param Actor|null $authority   (Optional) actor that must be the authority
33
     *                                of the returned statement
34
     *
35
     * @return Statement The statement
36
     *
37
     * @throws NotFoundException if no Statement with the given UUID does exist
38
     */
39 View Code Duplication
    final public function findStatementById($statementId, Actor $authority = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        $criteria = array('id' => $statementId);
42
43
        if (null !== $authority) {
44
            $criteria['authority'] = $authority;
45
        }
46
47
        $mappedStatement = $this->findMappedStatement($criteria);
48
49
        if (null === $mappedStatement) {
50
            throw new NotFoundException();
51
        }
52
53
        $statement = $mappedStatement->getModel();
54
55
        if ($statement->isVoidStatement()) {
56
            throw new NotFoundException();
57
        }
58
59
        return $statement;
60
    }
61
62
    /**
63
     * Finds a voided {@link Statement} by id.
64
     *
65
     * @param string     $voidedStatementId The voided statement id to filter
66
     *                                      by
67
     * @param Actor|null $authority         (Optional) actor that must be the
68
     *                                      authority of the returned statement
69
     *
70
     * @return Statement The statement
71
     *
72
     * @throws NotFoundException if no voided Statement with the given UUID
73
     *                           does exist
74
     */
75 View Code Duplication
    final public function findVoidedStatementById($voidedStatementId, Actor $authority = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $criteria = array('id' => $voidedStatementId);
78
79
        if (null !== $authority) {
80
            $criteria['authority'] = $authority;
81
        }
82
83
        $mappedStatement = $this->findMappedStatement($criteria);
84
85
        if (null === $mappedStatement) {
86
            throw new NotFoundException();
87
        }
88
89
        $statement = $mappedStatement->getModel();
90
91
        if (!$statement->isVoidStatement()) {
92
            throw new NotFoundException();
93
        }
94
95
        return $statement;
96
    }
97
98
    /**
99
     * Finds a collection of {@link Statement Statements} filtered by the given
100
     * criteria.
101
     *
102
     * @param StatementsFilter $criteria  The criteria to filter by
103
     * @param Actor|null       $authority (Optional) actor that must be the
104
     *                                    authority of the returned statements
105
     *
106
     * @return Statement[] The statements
107
     */
108
    final public function findStatementsBy(StatementsFilter $criteria, Actor $authority = null)
109
    {
110
        $criteria = $criteria->getFilter();
111
112
        if (null !== $authority) {
113
            $criteria['authority'] = $authority;
114
        }
115
116
        $mappedStatements = $this->findMappedStatements($criteria);
117
        $statements = array();
118
119
        foreach ($mappedStatements as $mappedStatement) {
120
            $statements[] = $mappedStatement->getModel();
121
        }
122
123
        return $statements;
124
    }
125
126
    /**
127
     * Writes a {@link Statement} to the underlying data storage.
128
     *
129
     * @param Statement $statement The statement to store
130
     * @param bool      $flush     Whether or not to flush the managed objects
131
     *                             immediately (i.e. write them to the data
132
     *                             storage)
133
     *
134
     * @return string The UUID of the created Statement
135
     */
136
    final public function storeStatement(Statement $statement, $flush = true)
137
    {
138
        $uuid = $statement->getId();
139
        $mappedStatement = MappedStatement::createFromModel($statement);
140
        $mappedStatement->stored = new \DateTime();
141
142
        if (null === $uuid) {
143
            $uuid = Uuid::uuid4()->toString();
144
            $mappedStatement->id = $uuid;
145
        }
146
147
        $this->storeMappedStatement($mappedStatement, $flush);
148
149
        return $uuid;
150
    }
151
152
    /**
153
     * Loads a certain {@link MappedStatement} from the data storage.
154
     *
155
     * @param array $criteria Criteria to filter a statement by
156
     *
157
     * @return MappedStatement The mapped statement
158
     */
159
    abstract protected function findMappedStatement(array $criteria);
160
161
    /**
162
     * Loads {@link MappedStatement mapped statements} from the data storage.
163
     *
164
     * @param array $criteria Criteria to filter statements by
165
     *
166
     * @return MappedStatement[] The mapped statements
167
     */
168
    abstract protected function findMappedStatements(array $criteria);
169
170
    /**
171
     * Writes a {@link MappedStatement mapped statement} to the underlying
172
     * data storage.
173
     *
174
     * @param MappedStatement $mappedStatement The statement to store
175
     * @param bool            $flush           Whether or not to flush the managed
176
     *                                         objects immediately (i.e. write
177
     *                                         them to the data storage)
178
     */
179
    abstract protected function storeMappedStatement(MappedStatement $mappedStatement, $flush);
180
}
181