Completed
Push — master ( ca406a...abda46 )
by
unknown
01:56
created

ConstraintRepositoryLookup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace WikibaseQuality\ConstraintReport;
4
5
use MediaWiki\Logger\LoggerFactory;
6
use Wikibase\DataModel\Entity\PropertyId;
7
use Wikimedia\Rdbms\ILoadBalancer;
8
use Wikimedia\Rdbms\IResultWrapper;
9
10
/**
11
 * @author BP2014N1
12
 * @license GPL-2.0-or-later
13
 */
14
class ConstraintRepositoryLookup implements ConstraintLookup {
15
16
	/** @var ILoadBalancer */
17
	private $lb;
18
19
	/** @var string|false */
20
	private $dbName;
21
22
	/**
23
	 * @param ILoadBalancer $lb Load balancer for database connections.
24
	 * Must match $dbName, i.e. if $dbName is not false,
25
	 * then using the main DBLoadBalancer service may be incorrect.
26
	 * @param string|false $dbName Database name ($domain for ILoadBalancer methods).
27
	 */
28
	public function __construct( ILoadBalancer $lb, $dbName ) {
29
		$this->lb = $lb;
30
		$this->dbName = $dbName;
31
	}
32
33
	/**
34
	 * @param PropertyId $propertyId
35
	 *
36
	 * @return Constraint[]
37
	 */
38 View Code Duplication
	public function queryConstraintsForProperty( PropertyId $propertyId ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
		$dbr = $this->lb->getConnectionRef( ILoadBalancer::DB_REPLICA, [], $this->dbName );
40
41
		$results = $dbr->select(
42
			'wbqc_constraints',
43
			'*',
44
			[ 'pid' => $propertyId->getNumericId() ]
45
		);
46
47
		return $this->convertToConstraints( $results );
48
	}
49
50
	/**
51
	 * @param IResultWrapper $results
52
	 *
53
	 * @return Constraint[]
54
	 */
55
	private function convertToConstraints( IResultWrapper $results ) {
56
		$constraints = [];
57
		$logger = LoggerFactory::getInstance( 'WikibaseQualityConstraints' );
58
		foreach ( $results as $result ) {
59
			$constraintTypeItemId = $result->constraint_type_qid;
60
			$constraintParameters = json_decode( $result->constraint_parameters, true );
61
62
			if ( $constraintParameters === null ) {
63
				// T171295
64
				$logger->warning( 'Constraint {constraintId} has invalid constraint parameters.', [
65
					'method' => __METHOD__,
66
					'constraintId' => $result->constraint_guid,
67
					'constraintParameters' => $result->constraint_parameters,
68
				] );
69
				$constraintParameters = [ '@error' => [ /* unknown */ ] ];
70
			}
71
72
			$constraints[] = new Constraint(
73
				$result->constraint_guid,
74
				PropertyId::newFromNumber( $result->pid ),
75
				$constraintTypeItemId,
76
				$constraintParameters
77
			);
78
		}
79
		return $constraints;
80
	}
81
82
}
83