TermStoreWriter::dropTermsForId()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.7333
c 0
b 0
f 0
cc 2
nc 3
nop 1
crap 2
1
<?php
2
3
namespace Queryr\TermStore;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\DBALException;
7
use Wikibase\DataModel\Entity\EntityId;
8
use Wikibase\DataModel\Term\AliasGroup;
9
use Wikibase\DataModel\Term\Fingerprint;
10
use Wikibase\DataModel\Term\Term;
11
12
/**
13
 * Package public
14
 * @since 0.2
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class TermStoreWriter {
20
21
	private $connection;
22
	private $config;
23
24 18
	public function __construct( Connection $connection, TermStoreConfig $config ) {
25 18
		$this->connection = $connection;
26 18
		$this->config = $config;
27 18
	}
28
29
	/**
30
	 * @param EntityId $id
31
	 * @param Fingerprint $fingerprint
32
	 *
33
	 * @throws TermStoreException
34
	 */
35 15
	public function storeEntityFingerprint( EntityId $id, Fingerprint $fingerprint ): void {
36 15
		$this->connection->beginTransaction();
37
38 15
		$this->dropTermsForId( $id );
39
40
		try {
41 14
			$this->storeFingerprintParts( $id, $fingerprint );
42
		}
43
		catch ( DBALException $ex ) {
44
			$this->connection->rollBack();
45
			throw new TermStoreException( $ex->getMessage(), $ex );
46
		}
47
48 14
		$this->connection->commit();
49 14
	}
50
51
	/**
52
	 * @throws DBALException
53
	 */
54 14
	private function storeFingerprintParts( EntityId $id, Fingerprint $fingerprint ) {
55
		/**
56
		 * @var Term $label
57
		 */
58 14
		foreach ( $fingerprint->getLabels() as $label ) {
59 14
			$this->storeLabel( $label->getLanguageCode(), $label->getText(), $id );
60
		}
61
62
		/**
63
		 * @var AliasGroup $aliasGroup
64
		 */
65 14
		foreach ( $fingerprint->getAliasGroups() as $aliasGroup ) {
66 4
			$this->storeAliases( $aliasGroup, $id );
67
		}
68 14
	}
69
70
	/**
71
	 * @param EntityId $id
72
	 *
73
	 * @throws TermStoreException
74
	 */
75 16
	public function dropTermsForId( EntityId $id ): void {
76
		try {
77 16
			$this->connection->delete(
78 16
				$this->config->getLabelTableName(),
79 16
				[ 'entity_id' => $id->getSerialization() ]
80
			);
81
82 14
			$this->connection->delete(
83 14
				$this->config->getAliasesTableName(),
84 14
				[ 'entity_id' => $id->getSerialization() ]
85
			);
86
		}
87 2
		catch ( DBALException $ex ) {
88 2
			throw new TermStoreException( $ex->getMessage(), $ex );
89
		}
90 14
	}
91
92
	/**
93
	 * @throws DBALException
94
	 */
95 14
	private function storeLabel( string $languageCode, string $text, EntityId $id ) {
96 14
		$this->connection->insert(
97 14
			$this->config->getLabelTableName(),
98
			[
99 14
				'text' => $text,
100 14
				'text_lowercase' => strtolower( $text ),
101 14
				'language' => $languageCode,
102 14
				'entity_id' => $id->getSerialization(),
103 14
				'entity_type' => $id->getEntityType()
104
			]
105
		);
106 14
	}
107
108
	/**
109
	 * @throws DBALException
110
	 */
111 4
	private function storeAliases( AliasGroup $aliasGroup, EntityId $id ) {
112 4
		foreach ( $aliasGroup->getAliases() as $alias ) {
113 4
			$this->connection->insert(
114 4
				$this->config->getAliasesTableName(),
115
				[
116 4
					'text' => $alias,
117 4
					'text_lowercase' => strtolower( $alias ),
118 4
					'language' => $aliasGroup->getLanguageCode(),
119 4
					'entity_id' => $id->getSerialization(),
120 4
					'entity_type' => $id->getEntityType()
121
				]
122
			);
123
		}
124 4
	}
125
126
}
127