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