Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
87.10% |
27 / 31 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
FormRevisionLookup | |
87.10% |
27 / 31 |
|
33.33% |
1 / 3 |
7.11 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getEntityRevision | |
72.73% |
8 / 11 |
|
0.00% |
0 / 1 |
4.32 | |||
getLatestRevisionId | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
2.00 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\DataAccess\Store; |
4 | |
5 | use OutOfRangeException; |
6 | use UnexpectedValueException; |
7 | use Wikibase\DataModel\Entity\EntityId; |
8 | use Wikibase\Lexeme\Domain\DummyObjects\NullFormId; |
9 | use Wikibase\Lexeme\Domain\Model\FormId; |
10 | use Wikibase\Lexeme\Domain\Model\Lexeme; |
11 | use Wikibase\Lib\Store\EntityRevision; |
12 | use Wikibase\Lib\Store\EntityRevisionLookup; |
13 | use Wikibase\Lib\Store\LatestRevisionIdResult; |
14 | use Wikibase\Lib\Store\LookupConstants; |
15 | use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException; |
16 | use Wikibase\Lib\Store\StorageException; |
17 | use Wikimedia\Assert\Assert; |
18 | |
19 | /** |
20 | * @license GPL-2.0-or-later |
21 | * @author Daniel Kinzler |
22 | * @author Thiemo Kreuz |
23 | */ |
24 | class FormRevisionLookup implements EntityRevisionLookup { |
25 | |
26 | /** |
27 | * @var EntityRevisionLookup |
28 | */ |
29 | private $lookup; |
30 | |
31 | public function __construct( EntityRevisionLookup $lookup ) { |
32 | $this->lookup = $lookup; |
33 | } |
34 | |
35 | /** |
36 | * @see EntityRevisionLookup::getEntityRevision |
37 | * |
38 | * @param FormId $formId |
39 | * @param int $revisionId |
40 | * @param string $mode |
41 | * |
42 | * @throws UnexpectedValueException |
43 | * @throws RevisionedUnresolvedRedirectException |
44 | * @throws StorageException |
45 | * @return EntityRevision|null |
46 | */ |
47 | public function getEntityRevision( |
48 | EntityId $formId, |
49 | $revisionId = 0, |
50 | $mode = LookupConstants::LATEST_FROM_REPLICA |
51 | ) { |
52 | Assert::parameterType( FormId::class, $formId, '$formId' ); |
53 | |
54 | if ( $formId instanceof NullFormId ) { |
55 | return null; |
56 | } |
57 | |
58 | $revision = $this->lookup->getEntityRevision( $formId->getLexemeId(), $revisionId, $mode ); |
59 | if ( $revision === null ) { |
60 | return null; |
61 | } |
62 | |
63 | /** @var Lexeme $lexeme */ |
64 | $lexeme = $revision->getEntity(); |
65 | '@phan-var Lexeme $lexeme'; |
66 | |
67 | try { |
68 | // TODO use hasForm on Lexeme or FormSet when it exists |
69 | $form = $lexeme->getForm( $formId ); |
70 | } catch ( OutOfRangeException $ex ) { |
71 | return null; |
72 | } |
73 | |
74 | return new EntityRevision( $form, $revision->getRevisionId(), $revision->getTimestamp() ); |
75 | } |
76 | |
77 | /** |
78 | * @see EntityRevisionLookup::getLatestRevisionId |
79 | * |
80 | * @param FormId $formId |
81 | * @param string $mode |
82 | * |
83 | * @throws UnexpectedValueException |
84 | * @return LatestRevisionIdResult|int|false |
85 | */ |
86 | public function getLatestRevisionId( |
87 | EntityId $formId, |
88 | $mode = LookupConstants::LATEST_FROM_REPLICA |
89 | ) { |
90 | Assert::parameterType( FormId::class, $formId, '$formId' ); |
91 | |
92 | $lexemeId = $formId->getLexemeId(); |
93 | |
94 | $revisionIdResult = $this->lookup->getLatestRevisionId( $lexemeId, $mode ); |
95 | |
96 | $returnNonexistentEntityResult = static function () { |
97 | return LatestRevisionIdResult::nonexistentEntity(); |
98 | }; |
99 | |
100 | return $revisionIdResult->onRedirect( $returnNonexistentEntityResult ) |
101 | ->onNonexistentEntity( $returnNonexistentEntityResult ) |
102 | ->onConcreteRevision( |
103 | function ( $revisionId, $revisionTimestamp ) use ( $lexemeId, $mode, $formId ) { |
104 | $revision = $this->lookup->getEntityRevision( $lexemeId, $revisionId, $mode ); |
105 | /** @var Lexeme $lexeme */ |
106 | $lexeme = $revision->getEntity(); |
107 | '@phan-var Lexeme $lexeme'; |
108 | |
109 | try { |
110 | $lexeme->getForm( $formId ); |
111 | } catch ( OutOfRangeException $ex ) { |
112 | return LatestRevisionIdResult::nonexistentEntity(); |
113 | } |
114 | |
115 | return LatestRevisionIdResult::concreteRevision( $revisionId, $revisionTimestamp ); |
116 | } |
117 | ) |
118 | ->map(); |
119 | } |
120 | |
121 | } |