PropertyStore::storePropertyRow()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0145

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 13
cp 0.8462
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0145
1
<?php
2
3
namespace Queryr\EntityStore;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\DBALException;
7
use Queryr\EntityStore\Data\PropertyInfo;
8
use Queryr\EntityStore\Data\PropertyRow;
9
use Wikibase\DataModel\Entity\PropertyId;
10
11
/**
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class PropertyStore {
16
17
	private $connection;
18
	private $tableName;
19
20
	/**
21
	 * This constructor is package private. Construction is done via EntityStoreFactory.
22
	 *
23
	 * @param Connection $connection
24
	 * @param string $tableName
25
	 */
26 19
	public function __construct( Connection $connection, $tableName ) {
27 19
		$this->connection = $connection;
28 19
		$this->tableName = $tableName;
29 19
	}
30
31
	/**
32
	 * @param PropertyRow $propertyRow
33
	 *
34
	 * @throws EntityStoreException
35
	 */
36 7
	public function storePropertyRow( PropertyRow $propertyRow ) {
37 7
		$this->deletePropertyById( PropertyId::newFromNumber( $propertyRow->getNumericPropertyId() ) );
38
39
		try {
40 5
			$this->connection->insert(
41 5
				$this->tableName,
42
				array(
43 5
					'property_id' => $propertyRow->getNumericPropertyId(),
44 5
					'property_json' => $propertyRow->getPropertyJson(),
45
46 5
					'page_title' => $propertyRow->getPageTitle(),
47 5
					'revision_id' => $propertyRow->getRevisionId(),
48 5
					'revision_time' => $propertyRow->getRevisionTime(),
49
50 5
					'property_type' => $propertyRow->getPropertyType(),
51
				)
52
			);
53
		}
54
		catch ( DBALException $ex ) {
55
			throw new EntityStoreException( $ex->getMessage(), $ex );
56
		}
57 5
	}
58
59
	/**
60
	 * @param PropertyId $propertyId
61
	 *
62
	 * @throws EntityStoreException
63
	 */
64 8
	public function deletePropertyById( PropertyId $propertyId ) {
65
		try {
66 8
			$this->connection->delete(
67 8
				$this->tableName,
68
				[
69 8
					'property_id' => $propertyId->getNumericId()
70
				]
71
			);
72
		}
73 3
		catch ( DBALException $ex ) {
74 3
			throw new EntityStoreException( $ex->getMessage(), $ex );
75
		}
76 5
	}
77
78
	/**
79
	 * @param string|int $numericPropertyId
80
	 *
81
	 * @return PropertyRow|null
82
	 * @throws EntityStoreException
83
	 */
84 6
	public function getPropertyRowByNumericPropertyId( $numericPropertyId ) {
85
		try {
86 6
			$rows = $this->selectProperties()
87 6
				->where( 'property_id = ?' )
88 6
				->setParameter( 0, (int)$numericPropertyId )
89 6
				->execute();
90
		}
91 2
		catch ( DBALException $ex ) {
92 2
			throw new EntityStoreException( $ex->getMessage(), $ex );
93
		}
94
95 4
		return $this->newPropertyRowFromResult( $rows );
0 ignored issues
show
Bug introduced by
It seems like $rows defined by $this->selectProperties(...cPropertyId)->execute() on line 86 can also be of type integer; however, Queryr\EntityStore\Prope...PropertyRowFromResult() does only seem to accept object<Traversable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
96
	}
97
98 6
	private function selectProperties() {
99 6
		return $this->connection->createQueryBuilder()->select(
100 6
			'property_id',
101 6
			'property_json',
102 6
			'page_title',
103 6
			'revision_id',
104 6
			'revision_time',
105 6
			'property_type'
106 6
		)->from( $this->tableName );
107
	}
108
109 4
	private function newPropertyRowFromResult( \Traversable $rows ) {
110 4
		$rows = iterator_to_array( $rows );
111
112 4
		if ( count( $rows ) < 1 ) {
113 2
			return null;
114
		}
115
116 2
		$row = reset( $rows );
117
118 2
		return new PropertyRow(
119 2
			$row['property_json'],
120 2
			$this->newPropertyInfoFromResultRow( $row )
121
		);
122
	}
123
124 4
	private function newPropertyInfoFromResultRow( array $row ) {
125 4
		return new PropertyInfo(
126 4
			$row['property_id'],
127 4
			$row['page_title'],
128 4
			$row['revision_id'],
129 4
			$row['revision_time'],
130 4
			$row['property_type']
131
		);
132
	}
133
134 5
	private function selectPropertyInfoSets() {
135 5
		return $this->connection->createQueryBuilder()->select(
136 5
			'property_id',
137 5
			'page_title',
138 5
			'revision_id',
139 5
			'revision_time',
140 5
			'property_type'
141 5
		)->from( $this->tableName );
142
	}
143
144
	/**
145
	 * @param int $limit
146
	 * @param int $offset
147
	 *
148
	 * @return PropertyInfo[]
149
	 * @throws EntityStoreException
150
	 */
151 5
	public function getPropertyInfo( $limit, $offset ) {
152
		try {
153 5
			$rows = $this->selectPropertyInfoSets()
154 5
				->orderBy( 'property_id', 'asc' )
155 5
				->setMaxResults( $limit )
156 5
				->setFirstResult( $offset )
157 5
				->execute();
158
		}
159 2
		catch ( DBALException $ex ) {
160 2
			throw new EntityStoreException( $ex->getMessage(), $ex );
161
		}
162
163 3
		return $this->newPropertyInfoArrayFromResult( $rows );
0 ignored issues
show
Bug introduced by
It seems like $rows defined by $this->selectPropertyInf...ult($offset)->execute() on line 153 can also be of type integer; however, Queryr\EntityStore\Prope...tyInfoArrayFromResult() does only seem to accept object<Traversable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
164
	}
165
166 3
	private function newPropertyInfoArrayFromResult( \Traversable $rows ) {
167 3
		$infoList = [];
168
169 3
		foreach ( $rows as $resultRow ) {
170 2
			$infoList[] = $this->newPropertyInfoFromResultRow( $resultRow );
171
		}
172
173 3
		return $infoList;
174
	}
175
176
}