Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.50% covered (success)
92.50%
37 / 40
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
CheckingResultsSource
92.50% covered (success)
92.50%
37 / 40
80.00% covered (warning)
80.00%
4 / 5
13.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getResults
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
1 / 1
7
 defaultResultsPerContext
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 defaultResultsPerEntity
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 statusSelected
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace WikibaseQuality\ConstraintReport\Api;
4
5use Wikibase\DataModel\Entity\EntityId;
6use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedCheckResults;
7use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\DependencyMetadata;
8use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\Metadata;
9use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
10use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\EntityContextCursor;
11use WikibaseQuality\ConstraintReport\ConstraintCheck\DelegatingConstraintChecker;
12use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
13use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\NullResult;
14
15/**
16 * @author Lucas Werkmeister
17 * @license GPL-2.0-or-later
18 */
19class 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[] $constraintIds
36     * @param string[] $statuses
37     * @return CachedCheckResults
38     */
39    public function getResults(
40        array $entityIds,
41        array $claimIds,
42        ?array $constraintIds,
43        array $statuses
44    ) {
45        $results = [];
46        $metadatas = [];
47        $statusesFlipped = array_flip( $statuses );
48        foreach ( $entityIds as $entityId ) {
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        foreach ( $claimIds as $claimId ) {
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}