Completed
Push — main ( b55b92...e8d442 )
by
unknown
05:41
created

RevisionGetter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 10 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 9
dl 10
loc 100
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Addwiki\Wikibase\Api\Service;
4
5
use Addwiki\Mediawiki\Api\Client\MediawikiApi;
6
use Addwiki\Mediawiki\Api\Client\SimpleRequest;
7
use Addwiki\Mediawiki\DataModel\PageIdentifier;
8
use Addwiki\Mediawiki\DataModel\Revision;
9
use Addwiki\Wikibase\DataModel\ItemContent;
10
use Addwiki\Wikibase\DataModel\PropertyContent;
11
use Deserializers\Deserializer;
12
use RuntimeException;
13
use Wikibase\DataModel\Entity\EntityId;
14
use Wikibase\DataModel\Entity\Item;
15
use Wikibase\DataModel\Entity\Property;
16
use Wikibase\DataModel\SiteLink;
17
18
/**
19
 * @access private
20
 *
21
 * @author Addshore
22
 */
23
class RevisionGetter {
24
25
	protected MediawikiApi $api;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
26
27
	protected \Deserializers\Deserializer $entityDeserializer;
28
29
	/**
30
	 * @param MediawikiApi $api
31
	 * @param Deserializer $entityDeserializer
32
	 */
33
	public function __construct( MediawikiApi $api, Deserializer $entityDeserializer ) {
34
		$this->api = $api;
35
		$this->entityDeserializer = $entityDeserializer;
36
	}
37
38
	/**
39
	 * @since 0.1
40
	 * @param string|EntityId $id
41
	 */
42
	public function getFromId( $id ): Revision {
43
		if ( $id instanceof EntityId ) {
44
			$id = $id->getSerialization();
45
		}
46
47
		$result = $this->api->getRequest( new SimpleRequest( 'wbgetentities', [ 'ids' => $id ] ) );
48
		return $this->newRevisionFromResult( array_shift( $result['entities'] ) );
49
	}
50
51
	/**
52
	 * @since 0.1
53
	 * @param SiteLink $siteLink
54
	 */
55
	public function getFromSiteLink( SiteLink $siteLink ): Revision {
56
		$result = $this->api->getRequest( new SimpleRequest(
57
			'wbgetentities',
58
			[ 'sites' => $siteLink->getSiteId(), 'titles' => $siteLink->getPageName() ]
59
		) );
60
		return $this->newRevisionFromResult( array_shift( $result['entities'] ) );
61
	}
62
63
	/**
64
	 * @since 0.1
65
	 */
66
	public function getFromSiteAndTitle( string $siteId, string $title ): Revision {
67
		$result = $this->api->getRequest( new SimpleRequest(
68
			'wbgetentities',
69
			[ 'sites' => $siteId, 'titles' => $title ]
70
		) );
71
		return $this->newRevisionFromResult( array_shift( $result['entities'] ) );
72
	}
73
74
	/**
75
	 * @param array $entityResult
76
	 * @return bool|Revision
77
	 * @todo this could be factored into a different class?
78
	 */
79
	private function newRevisionFromResult( array $entityResult ) {
80
		if ( array_key_exists( 'missing', $entityResult ) ) {
81
			return false; // Throw an exception?
82
		}
83
		return new Revision(
84
			$this->getContentFromEntity( $this->entityDeserializer->deserialize( $entityResult ) ),
85
			new PageIdentifier( null, (int)$entityResult['pageid'] ),
86
			$entityResult['lastrevid'],
87
			null,
88
			null,
89
			$entityResult['modified']
90
		);
91
	}
92
93
	/**
94
	 * @param Item|Property $entity
95
	 *
96
	 * @throws RuntimeException
97
	 * @return ItemContent|PropertyContent|void
98
	 * @todo this could be factored into a different class?
99
	 */
100
	private function getContentFromEntity( $entity ) {
101
		switch ( $entity->getType() ) {
102
			case Item::ENTITY_TYPE:
103
				return new ItemContent( $entity );
104
			case Property::ENTITY_TYPE:
105
				return new PropertyContent( $entity );
106
			default:
107
				throw new RuntimeException( 'I cant get a content for this type of entity' );
108
		}
109
	}
110
111
}
112