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