Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.66% |
26 / 29 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| SenseRevisionLookup | |
89.66% |
26 / 29 |
|
66.67% |
2 / 3 |
7.05 | |
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 | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
2 | |||
| 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 ) { |
| 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 = LatestRevisionIdResult::nonexistentEntity( ... ); |
| 94 | |
| 95 | return $revisionIdResult->onRedirect( $returnNonexistentEntityResult ) |
| 96 | ->onNonexistentEntity( $returnNonexistentEntityResult ) |
| 97 | ->onConcreteRevision( |
| 98 | function ( $revisionId, $revisionTimestamp ) use ( $lexemeId, $mode, $senseId ) { |
| 99 | $revision = $this->lookup->getEntityRevision( $lexemeId, $revisionId, $mode ); |
| 100 | /** @var Lexeme $lexeme */ |
| 101 | $lexeme = $revision->getEntity(); |
| 102 | '@phan-var Lexeme $lexeme'; |
| 103 | |
| 104 | try { |
| 105 | $lexeme->getSense( $senseId ); |
| 106 | } catch ( OutOfRangeException ) { |
| 107 | return LatestRevisionIdResult::nonexistentEntity(); |
| 108 | } |
| 109 | |
| 110 | return LatestRevisionIdResult::concreteRevision( $revisionId, $revisionTimestamp ); |
| 111 | } |
| 112 | ) |
| 113 | ->map(); |
| 114 | } |
| 115 | |
| 116 | } |