FederatedPropertiesEntitySourceDefinitionsConfigParser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 91
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getLocalEntitySource() 0 14 2
A initializeDefaults() 0 46 3
1
<?php
2
3
declare( strict_types=1 );
4
namespace Wikibase\Repo\FederatedProperties;
5
6
use InvalidArgumentException;
7
use Wikibase\DataAccess\EntitySource;
8
use Wikibase\DataAccess\EntitySourceDefinitions;
9
use Wikibase\DataModel\Entity\Property;
10
use Wikibase\Lib\EntityTypeDefinitions;
11
use Wikibase\Lib\SettingsArray;
12
13
/**
14
 * A class to initialize default entitySource values for federated properties
15
 *
16
 * This is currently only used when the source wiki is set to it's default value.
17
 *
18
 * @license GPL-2.0-or-later
19
 *
20
 * @author Tobias Andersson
21
 */
22
class FederatedPropertiesEntitySourceDefinitionsConfigParser {
23
24
	/**
25
	 * @var string
26
	 */
27
	private $sourceScriptUrl;
28
29
	/**
30
	 * @var string
31
	 */
32
	private $localEntitySourceName;
33
34
	public function __construct( SettingsArray $settings ) {
35
		$this->sourceScriptUrl = $settings->getSetting( 'federatedPropertiesSourceScriptUrl' );
36
		$this->localEntitySourceName = $settings->getSetting( 'localEntitySourceName' );
37
	}
38
39
	/**
40
	 * @param EntitySource[] $sources
41
	 * @return EntitySource
42
	 */
43
	private function getLocalEntitySource( array $sources ) : EntitySource {
44
		$result = array_filter(
45
			$sources,
46
			function ( $entitySource ) {
47
				return $entitySource->getSourceName() === $this->localEntitySourceName;
48
			}
49
		);
50
51
		if ( empty( $result ) ) {
52
			throw new InvalidArgumentException( 'No entity sources defined for "' . $this->localEntitySourceName . '"' );
53
		}
54
55
		return $result[0];
56
	}
57
58
	/**
59
	 * If the source wiki is set to it's default value we can setup the entity sources automatically
60
	 * based on what we know of the setup of www.wikidata.org
61
	 *
62
	 * @param EntitySourceDefinitions $definitions
63
	 * @param EntityTypeDefinitions $entityTypeDefinitions
64
	 * @return EntitySourceDefinitions
65
	 */
66
	public function initializeDefaults( EntitySourceDefinitions $definitions, EntityTypeDefinitions $entityTypeDefinitions ) {
67
68
		if ( $this->sourceScriptUrl !== 'https://www.wikidata.org/w/' ) {
69
			return $definitions;
70
		}
71
72
		$definitions->getSources();
0 ignored issues
show
Unused Code introduced by
The call to the method Wikibase\DataAccess\Enti...finitions::getSources() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
73
		$defaultLocal = $this->getLocalEntitySource( $definitions->getSources() );
74
75
		$entityTypes = $defaultLocal->getEntityTypes();
76
		$entityNamespaceIds = $defaultLocal->getEntityNamespaceIds();
77
		$entitySlots = $defaultLocal->getEntitySlotNames();
78
79
		$entityNamespaceIdsAndSlots = [];
80
		foreach ( $entityTypes as $entityType ) {
81
			$entityNamespaceIdsAndSlots[$entityType] = [
82
				'namespaceId' => $entityNamespaceIds[$entityType],
83
				'slot' => $entitySlots[$entityType]
84
			];
85
		}
86
87
		$propertyNamespaceIdAndSlot = $entityNamespaceIdsAndSlots[Property::ENTITY_TYPE];
88
		unset( $entityNamespaceIdsAndSlots[ Property::ENTITY_TYPE] );
89
90
		$newLocal = new EntitySource(
91
			$defaultLocal->getSourceName(),
92
			$defaultLocal->getDatabaseName(),
93
			$entityNamespaceIdsAndSlots,
94
			$defaultLocal->getConceptBaseUri(),
95
			$defaultLocal->getRdfNodeNamespacePrefix(),
96
			$defaultLocal->getRdfPredicateNamespacePrefix(),
97
			$defaultLocal->getInterwikiPrefix()
98
		);
99
100
		$fedPropsSource = new EntitySource(
101
			'fedprops',
102
			false,
103
			[ Property::ENTITY_TYPE => $propertyNamespaceIdAndSlot ],
104
			'http://www.wikidata.org/entity/',
105
			'fpwd',
106
			'fpwd',
107
			'wikidata'
108
		);
109
110
		return new EntitySourceDefinitions( [ $newLocal, $fedPropsSource ], $entityTypeDefinitions );
111
	}
112
}
113