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\Doctrine\Repository; |
13
|
|
|
|
14
|
|
|
use Rhumsaa\Uuid\Uuid as RhumsaaUuid; |
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 Xabbuh\XApi\Model\Uuid as ModelUuid; |
21
|
|
|
use XApi\Repository\Api\StatementRepositoryInterface; |
22
|
|
|
use XApi\Repository\Doctrine\Mapping\Statement as MappedStatement; |
23
|
|
|
use XApi\Repository\Doctrine\Repository\Mapping\StatementRepository as MappedStatementRepository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Doctrine based {@link Statement} repository. |
27
|
|
|
* |
28
|
|
|
* @author Christian Flothmann <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class StatementRepository implements StatementRepositoryInterface |
31
|
|
|
{ |
32
|
|
|
private $repository; |
33
|
|
|
|
34
|
|
|
public function __construct(MappedStatementRepository $repository) |
35
|
|
|
{ |
36
|
|
|
$this->repository = $repository; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function findStatementById(StatementId $statementId, Actor $authority = null) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$criteria = array('id' => $statementId->getValue()); |
45
|
|
|
|
46
|
|
|
if (null !== $authority) { |
47
|
|
|
$criteria['authority'] = $authority; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$mappedStatement = $this->repository->findStatement($criteria); |
51
|
|
|
|
52
|
|
|
if (null === $mappedStatement) { |
53
|
|
|
throw new NotFoundException('No statements could be found matching the given criteria.'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$statement = $mappedStatement->getModel(); |
57
|
|
|
|
58
|
|
|
if ($statement->isVoidStatement()) { |
59
|
|
|
throw new NotFoundException('The stored statement is a voiding statement.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $statement; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function findVoidedStatementById(StatementId $voidedStatementId, Actor $authority = null) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$criteria = array('id' => $voidedStatementId->getValue()); |
71
|
|
|
|
72
|
|
|
if (null !== $authority) { |
73
|
|
|
$criteria['authority'] = $authority; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$mappedStatement = $this->repository->findStatement($criteria); |
77
|
|
|
|
78
|
|
|
if (null === $mappedStatement) { |
79
|
|
|
throw new NotFoundException('No voided statements could be found matching the given criteria.'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$statement = $mappedStatement->getModel(); |
83
|
|
|
|
84
|
|
|
if (!$statement->isVoidStatement()) { |
85
|
|
|
throw new NotFoundException('The stored statement is no voiding statement.'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $statement; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function findStatementsBy(StatementsFilter $criteria, Actor $authority = null) |
95
|
|
|
{ |
96
|
|
|
$criteria = $criteria->getFilter(); |
97
|
|
|
|
98
|
|
|
if (null !== $authority) { |
99
|
|
|
$criteria['authority'] = $authority; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$mappedStatements = $this->repository->findStatements($criteria); |
103
|
|
|
$statements = array(); |
104
|
|
|
|
105
|
|
|
foreach ($mappedStatements as $mappedStatement) { |
106
|
|
|
$statements[] = $mappedStatement->getModel(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $statements; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function storeStatement(Statement $statement, $flush = true) |
116
|
|
|
{ |
117
|
|
|
if (null === $statement->getId()) { |
118
|
|
|
if (class_exists('Xabbuh\XApi\Model\Uuid')) { |
119
|
|
|
$uuid = ModelUuid::uuid4(); |
120
|
|
|
} else { |
121
|
|
|
$uuid = RhumsaaUuid::uuid4(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$statement = $statement->withId(StatementId::fromUuid($uuid)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$mappedStatement = MappedStatement::fromModel($statement); |
128
|
|
|
$mappedStatement->stored = time(); |
129
|
|
|
|
130
|
|
|
$this->repository->storeStatement($mappedStatement, $flush); |
131
|
|
|
|
132
|
|
|
return $statement->getId(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
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.