MongoDBEntityDatabase   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 14
c 5
b 1
f 0
lcom 1
cbo 6
dl 0
loc 102
ccs 51
cts 51
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A buildGetEntityForIdQuery() 0 4 1
A buildGetEntitiesForIdsQuery() 0 4 1
A getEntityDocumentForId() 0 7 2
A getEntityDocumentsForIds() 0 12 2
A splitEntityIdsPerType() 0 10 2
A getEntitiesInCollection() 0 11 2
A saveEntityDocument() 0 6 1
A serializeEntityIds() 0 10 2
1
<?php
2
3
namespace Wikibase\EntityStore\MongoDB;
4
5
use Doctrine\MongoDB\Database;
6
use Doctrine\MongoDB\Query\Expr;
7
use Wikibase\DataModel\Entity\EntityDocument;
8
use Wikibase\DataModel\Entity\EntityId;
9
use Wikibase\EntityStore\EntityDocumentLookup;
10
use Wikibase\EntityStore\EntityDocumentSaver;
11
12
/**
13
 * Internal class
14
 *
15
 * @licence GPLv2+
16
 * @author Thomas Pellissier Tanon
17
 */
18
class MongoDBEntityDatabase implements EntityDocumentLookup, EntityDocumentSaver {
19
20
	/**
21
	 * @var Database
22
	 */
23
	private $database;
24
25
	/**
26
	 * @var MongoDBDocumentBuilder
27
	 */
28
	private $documentBuilder;
29
30
	/**
31
	 * @param Database $database
32
	 * @param MongoDBDocumentBuilder $documentBuilder
33
	 */
34 5
	public function __construct( Database $database, MongoDBDocumentBuilder $documentBuilder ) {
35 5
		$this->database = $database;
36 5
		$this->documentBuilder = $documentBuilder;
37 5
	}
38
39
	/**
40
	 * @see EntityDocumentLookup::getEntityDocumentForId
41
	 */
42 3
	public function getEntityDocumentForId( EntityId $entityId ) {
43 3
		$document = $this->database
44 3
			->selectCollection( $entityId->getEntityType() )
45 3
			->findOne( $this->buildGetEntityForIdQuery( $entityId ) );
0 ignored issues
show
Documentation introduced by
$this->buildGetEntityForIdQuery($entityId) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
47 3
		return ( $document === null ) ? null : $this->documentBuilder->buildEntityForDocument( $document );
48
	}
49
50
	/**
51
	 * @see EntityDocumentLookup::getEntityDocumentsForIds
52
	 */
53 2
	public function getEntityDocumentsForIds( array $entityIds ) {
54 2
		$entities = [];
55
56 2
		foreach( $this->splitEntityIdsPerType( $entityIds ) as $type => $entityIdsForType ) {
57 2
			$entities = array_merge(
58 2
				$entities,
59 2
				$this->getEntitiesInCollection( $entityIdsForType, $type )
60 2
			);
61 2
		}
62
63 2
		return $entities;
64
	}
65
66 2
	private function splitEntityIdsPerType( array $entityIds ) {
67 2
		$entityIdsPerType = [];
68
69
		/** @var EntityId $entityId */
70 2
		foreach( $entityIds as $entityId ) {
71 2
			$entityIdsPerType[$entityId->getEntityType()][] = $entityId;
72 2
		}
73
74 2
		return $entityIdsPerType;
75
	}
76
77 2
	private function getEntitiesInCollection( array $entityIds, $collectionName ) {
78 2
		$documents = $this->database
79 2
			->selectCollection( $collectionName )
80 2
			->find( $this->buildGetEntitiesForIdsQuery( $entityIds ) );
0 ignored issues
show
Documentation introduced by
$this->buildGetEntitiesForIdsQuery($entityIds) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
82 2
		$entities = [];
83 2
		foreach( $documents as $document ) {
84 1
			$entities[] = $this->documentBuilder->buildEntityForDocument( $document );
85 2
		}
86 2
		return $entities;
87
	}
88
89
	/**
90
	 * @see EntityDocumentSaver::saveEntityDocument
91
	 */
92 2
	public function saveEntityDocument( EntityDocument $entityDocument ) {
93 2
		$this->database->selectCollection( $entityDocument->getType() )->upsert(
94 2
			$this->buildGetEntityForIdQuery( $entityDocument->getId() ),
0 ignored issues
show
Bug introduced by
It seems like $entityDocument->getId() can be null; however, buildGetEntityForIdQuery() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Documentation introduced by
$this->buildGetEntityFor...ntityDocument->getId()) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95 2
			$this->documentBuilder->buildDocumentForEntity( $entityDocument )
96 2
		);
97 2
	}
98
99 4
	private function buildGetEntityForIdQuery( EntityId $entityId ) {
100 4
		$expr = new Expr();
101 4
		return $expr->field( '_id' )->equals( $entityId->getSerialization() )->getQuery();
102
	}
103
104 2
	private function buildGetEntitiesForIdsQuery( array $entityIds ) {
105 2
		$expr = new Expr();
106 2
		return $expr->field( '_id' )->in( $this->serializeEntityIds( $entityIds ) )->getQuery();
107
	}
108
109 2
	private function serializeEntityIds( array $entityIds ) {
110 2
		$serializations = [];
111
112
		/** @var EntityId $entityId */
113 2
		foreach( $entityIds as $entityId ) {
114 2
			$serializations[] = $entityId->getSerialization();
115 2
		}
116
117 2
		return $serializations;
118
	}
119
}
120