Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
87.10% |
27 / 31 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
SenseRevisionLookup | |
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\NullSenseId; |
9 | use Wikibase\Lexeme\Domain\Model\Lexeme; |
10 | use Wikibase\Lexeme\Domain\Model\SenseId; |
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 | */ |
22 | class SenseRevisionLookup implements EntityRevisionLookup { |
23 | |
24 | /** |
25 | * @var EntityRevisionLookup |
26 | */ |
27 | private $lookup; |
28 | |
29 | public function __construct( EntityRevisionLookup $lookup ) { |
30 | $this->lookup = $lookup; |
31 | } |
32 | |
33 | /** |
34 | * @see EntityRevisionLookup::getEntityRevision |
35 | * |
36 | * @param SenseId $senseId |
37 | * @param int $revisionId |
38 | * @param string $mode |
39 | * |
40 | * @throws UnexpectedValueException |
41 | * @throws RevisionedUnresolvedRedirectException |
42 | * @throws StorageException |
43 | * @return EntityRevision|null |
44 | */ |
45 | public function getEntityRevision( |
46 | EntityId $senseId, |
47 | $revisionId = 0, |
48 | $mode = LookupConstants::LATEST_FROM_REPLICA |
49 | ) { |
50 | Assert::parameterType( SenseId::class, $senseId, '$senseId' ); |
51 | |
52 | if ( $senseId instanceof NullSenseId ) { |
53 | return null; |
54 | } |
55 | |
56 | $revision = $this->lookup->getEntityRevision( $senseId->getLexemeId(), $revisionId, $mode ); |
57 | if ( $revision === null ) { |
58 | return null; |
59 | } |
60 | |
61 | /** @var Lexeme $lexeme */ |
62 | $lexeme = $revision->getEntity(); |
63 | '@phan-var Lexeme $lexeme'; |
64 | |
65 | try { |
66 | // TODO use hasSense on Lexeme or SenseSet when it exists |
67 | $sense = $lexeme->getSense( $senseId ); |
68 | } catch ( OutOfRangeException $ex ) { |
69 | return null; |
70 | } |
71 | |
72 | return new EntityRevision( $sense, $revision->getRevisionId(), $revision->getTimestamp() ); |
73 | } |
74 | |
75 | /** |
76 | * @see EntityRevisionLookup::getLatestRevisionId |
77 | * |
78 | * @param SenseId $senseId |
79 | * @param string $mode |
80 | * |
81 | * @throws UnexpectedValueException |
82 | * @return LatestRevisionIdResult|int|false |
83 | */ |
84 | public function getLatestRevisionId( |
85 | EntityId $senseId, |
86 | $mode = LookupConstants::LATEST_FROM_REPLICA |
87 | ) { |
88 | Assert::parameterType( SenseId::class, $senseId, '$senseId' ); |
89 | |
90 | $lexemeId = $senseId->getLexemeId(); |
91 | $revisionIdResult = $this->lookup->getLatestRevisionId( $lexemeId, $mode ); |
92 | |
93 | $returnNonexistentEntityResult = static function () { |
94 | return LatestRevisionIdResult::nonexistentEntity(); |
95 | }; |
96 | |
97 | return $revisionIdResult->onRedirect( $returnNonexistentEntityResult ) |
98 | ->onNonexistentEntity( $returnNonexistentEntityResult ) |
99 | ->onConcreteRevision( |
100 | function ( $revisionId, $revisionTimestamp ) use ( $lexemeId, $mode, $senseId ) { |
101 | $revision = $this->lookup->getEntityRevision( $lexemeId, $revisionId, $mode ); |
102 | /** @var Lexeme $lexeme */ |
103 | $lexeme = $revision->getEntity(); |
104 | '@phan-var Lexeme $lexeme'; |
105 | |
106 | try { |
107 | $lexeme->getSense( $senseId ); |
108 | } catch ( OutOfRangeException $ex ) { |
109 | return LatestRevisionIdResult::nonexistentEntity(); |
110 | } |
111 | |
112 | return LatestRevisionIdResult::concreteRevision( $revisionId, $revisionTimestamp ); |
113 | } |
114 | ) |
115 | ->map(); |
116 | } |
117 | |
118 | } |