Completed
Push — master ( 8bf143...89dc24 )
by
unknown
02:53
created

DumpMetaInformation::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 21
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace WikibaseQuality\ExternalValidation\DumpMetaInformation;
4
5
use Language;
6
use InvalidArgumentException;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikimedia\Assert\Assert;
10
11
/**
12
 * @package WikibaseQuality\ExternalValidation\DumpMetaInformation
13
 * @author BP2014N1
14
 * @license GNU GPL v2+
15
 */
16
class DumpMetaInformation {
17
18
	/**
19
	 * Id of the dump
20
	 *
21
	 * @var string
22
	 */
23
	private $dumpId;
24
25
	/**
26
	 * Id of the item that represents the data source of the dump
27
	 *
28
	 * @var ItemId
29
	 */
30
	private $sourceItemId;
31
32
	/**
33
	 * Properties for identifiers of the dump.
34
	 *
35
	 * @var PropertyId[]
36
	 */
37
	private $identifierPropertyIds;
38
39
	/**
40
	 * Date of import
41
	 *
42
	 * @var string
43
	 */
44
	private $importDate;
45
46
	/**
47
	 * Language code of values of the dump
48
	 *
49
	 * @var string
50
	 */
51
	private $languageCode;
52
53
	/**
54
	 * Source url of the downloaded dump
55
	 *
56
	 * @var string
57
	 */
58
	private $sourceUrl;
59
60
	/**
61
	 * Size of the imported dump in byte
62
	 *
63
	 * @var int
64
	 */
65
	private $size;
66
67
	/**
68
	 * Id of the item that represents the license of the database
69
	 *
70
	 * @var ItemId
71
	 */
72
	private $licenseItemId;
73
74
	/**
75
	 * @param string $dumpId
76
	 * @param ItemId $sourceItemId
77
	 * @param PropertyId[] $identifierPropertyIds
78
	 * @param string $importDate
79
	 * @param string $languageCode
80
	 * @param string $sourceUrl
81
	 * @param int $size
82
	 * @param ItemId $licenseItemId
83
	 *
84
	 * @throws InvalidArgumentException
85
	 */
86
	public function __construct(
87
		$dumpId,
88
		ItemId $sourceItemId,
89
		array $identifierPropertyIds,
90
		$importDate,
91
		$languageCode,
92
		$sourceUrl,
93
		$size,
94
		ItemId $licenseItemId
95
	) {
96
		Assert::parameterElementType(
97
			PropertyId::class,
98
			$identifierPropertyIds,
99
			'$identifierPropertyIds'
100
		);
101
102
		$this->setDumpId( $dumpId );
103
		$this->sourceItemId = $sourceItemId;
104
		$this->identifierPropertyIds = $identifierPropertyIds;
105
		$this->setImportDate( $importDate );
106
		$this->setLanguageCode( $languageCode );
107
		$this->setSourceUrl( $sourceUrl );
108
		$this->setSize( $size );
109
		$this->licenseItemId = $licenseItemId;
110
	}
111
112
	/**
113
	 * @return string
114
	 */
115
	public function getDumpId() {
116
		return $this->dumpId;
117
	}
118
119
	/**
120
	 * @return ItemId
121
	 */
122
	public function getSourceItemId() {
123
		return $this->sourceItemId;
124
	}
125
126
	/**
127
	 * @return \Wikibase\DataModel\Entity\PropertyId[]
128
	 */
129
	public function getIdentifierPropertyIds() {
130
		return $this->identifierPropertyIds;
131
	}
132
133
	/**
134
	 * @return string
135
	 */
136
	public function getImportDate() {
137
		return $this->importDate;
138
	}
139
140
	/**
141
	 * @return string
142
	 */
143
	public function getLanguageCode() {
144
		return $this->languageCode;
145
	}
146
147
	/**
148
	 * @return string
149
	 */
150
	public function getSourceUrl() {
151
		return $this->sourceUrl;
152
	}
153
154
	/**
155
	 * @return int
156
	 */
157
	public function getSize() {
158
		return $this->size;
159
	}
160
161
	/**
162
	 * @return ItemId
163
	 */
164
	public function getLicenseItemId() {
165
		return $this->licenseItemId;
166
	}
167
168
	/**
169
	 * @param string $dumpId
170
	 *
171
	 * @throws InvalidArgumentException
172
	 */
173
	private function setDumpId( $dumpId ) {
174
		Assert::parameterType( 'string', $dumpId, '$dumpId' );
175
		$length = strlen( $dumpId );
176
		if ( $length < 1 || $length > 25 ) {
177
			throw new InvalidArgumentException( '$dumpId must be between 1 and 25 characters.' );
178
		}
179
180
		$this->dumpId = $dumpId;
181
	}
182
183
	/**
184
	 * @param string $languageCode
185
	 *
186
	 * @throws InvalidArgumentException
187
	 */
188
	private function setLanguageCode( $languageCode ) {
189
		Assert::parameterType( 'string', $languageCode, '$languageCode' );
190
		if ( !Language::isValidCode( $languageCode ) ) {
191
			throw new InvalidArgumentException( '$languageCode is not valid.' );
192
		}
193
194
		$this->languageCode = $languageCode;
195
	}
196
197
	/**
198
	 * @param string $importDate
199
	 *
200
	 * @throws InvalidArgumentException
201
	 */
202
	private function setImportDate( $importDate ) {
203
		Assert::parameterType( 'string', $importDate, '$importDate' );
204
205
		$timestamp = wfTimestamp( TS_MW, $importDate );
206
		if ( !$timestamp ) {
207
			throw new InvalidArgumentException( '$updatedAt has invalid timestamp format.' );
208
		}
209
210
		$this->importDate = $importDate;
211
	}
212
213
	/**
214
	 * @param string $sourceUrl
215
	 *
216
	 * @throws InvalidArgumentException
217
	 */
218
	private function setSourceUrl( $sourceUrl ) {
219
		Assert::parameterType( 'string', $sourceUrl, '$sourceUrl' );
220
		if ( strlen( $sourceUrl ) > 300 ) {
221
			throw new InvalidArgumentException( '$sourceUrl must not be longer than 300 characters.' );
222
		}
223
		if ( !filter_var( $sourceUrl, FILTER_VALIDATE_URL ) ) {
224
			throw new InvalidArgumentException( '$sourceUrl is not a valid url.' );
225
		}
226
227
		$this->sourceUrl = $sourceUrl;
228
	}
229
230
	/**
231
	 * @param int $size
232
	 *
233
	 * @throws InvalidArgumentException
234
	 */
235
	private function setSize( $size ) {
236
		Assert::parameterType( 'integer', $size, '$size' );
237
		if ( $size <= 0 ) {
238
			throw new InvalidArgumentException( '$size must be positive integer.' );
239
		}
240
241
		$this->size = $size;
242
	}
243
244
}
245