Completed
Push — master ( f2f357...ccb861 )
by
unknown
02:21
created

CheckingResultsSource::getResults()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 41

Duplication

Lines 27
Ratio 65.85 %

Importance

Changes 0
Metric Value
dl 27
loc 41
rs 8.3306
c 0
b 0
f 0
cc 7
nc 16
nop 4
1
<?php
2
3
namespace WikibaseQuality\ConstraintReport\Api;
4
5
use Wikibase\DataModel\Entity\EntityId;
6
use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedCheckResults;
7
use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\DependencyMetadata;
8
use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\Metadata;
9
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
10
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\EntityContextCursor;
11
use WikibaseQuality\ConstraintReport\ConstraintCheck\DelegatingConstraintChecker;
12
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
13
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\NullResult;
14
15
/**
16
 * @author Lucas Werkmeister
17
 * @license GPL-2.0-or-later
18
 */
19
class CheckingResultsSource implements ResultsSource {
20
21
	/**
22
	 * @var DelegatingConstraintChecker
23
	 */
24
	private $delegatingConstraintChecker;
25
26
	public function __construct(
27
		DelegatingConstraintChecker $delegatingConstraintChecker
28
	) {
29
		$this->delegatingConstraintChecker = $delegatingConstraintChecker;
30
	}
31
32
	/**
33
	 * @param EntityId[] $entityIds
34
	 * @param string[] $claimIds
35
	 * @param string[]|null $constraintIds
36
	 * @param string[] $statuses
37
	 * @return CachedCheckResults
38
	 */
39
	public function getResults(
40
		array $entityIds,
41
		array $claimIds,
42
		array $constraintIds = null,
43
		array $statuses
44
	) {
45
		$results = [];
46
		$metadatas = [];
47
		$statusesFlipped = array_flip( $statuses );
48 View Code Duplication
		foreach ( $entityIds as $entityId ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
49
			$entityResults = $this->delegatingConstraintChecker->checkAgainstConstraintsOnEntityId(
50
				$entityId,
51
				$constraintIds,
52
				[ $this, 'defaultResultsPerContext' ],
53
				[ $this, 'defaultResultsPerEntity' ]
54
			);
55
			foreach ( $entityResults as $result ) {
56
				$metadatas[] = $result->getMetadata();
57
				if ( $this->statusSelected( $statusesFlipped, $result ) ) {
58
					$results[] = $result;
59
				}
60
			}
61
		}
62 View Code Duplication
		foreach ( $claimIds as $claimId ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63
			$claimResults = $this->delegatingConstraintChecker->checkAgainstConstraintsOnClaimId(
64
				$claimId,
65
				$constraintIds,
66
				[ $this, 'defaultResultsPerContext' ]
67
			);
68
			foreach ( $claimResults as $result ) {
69
				$metadatas[] = $result->getMetadata();
70
				if ( $this->statusSelected( $statusesFlipped, $result ) ) {
71
					$results[] = $result;
72
				}
73
			}
74
		}
75
		return new CachedCheckResults(
76
			$results,
77
			Metadata::merge( $metadatas )
78
		);
79
	}
80
81
	public function defaultResultsPerContext( Context $context ) {
82
		return $context->getType() === Context::TYPE_STATEMENT ?
83
			[ new NullResult( $context->getCursor() ) ] :
84
			[];
85
	}
86
87
	public function defaultResultsPerEntity( EntityId $entityId ) {
88
		return [
89
			( new NullResult( new EntityContextCursor( $entityId->getSerialization() ) ) )
90
				->withMetadata( Metadata::ofDependencyMetadata(
91
					DependencyMetadata::ofEntityId( $entityId )
92
				) )
93
		];
94
	}
95
96
	public function statusSelected( array $statusesFlipped, CheckResult $result ) {
97
		return array_key_exists( $result->getStatus(), $statusesFlipped ) ||
98
			$result instanceof NullResult;
99
	}
100
101
}
102