Passed
Push — master ( e00f3d...9a419b )
by Enrico
01:37
created

LocalBusiness::asTurtleFragment()   F

Complexity

Conditions 35
Paths > 20000

Size

Total Lines 95
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 35

Importance

Changes 0
Metric Value
dl 0
loc 95
ccs 14
cts 14
cp 1
rs 2.0338
c 0
b 0
f 0
cc 35
eloc 66
nc 67108865
nop 0
crap 35

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace BOTK\Model;
3
4
5
/**
6
 * An ibrid class that merge the semantic of schema:organization, schema:place and schema:geo, 
7
 * it is similar to schema:LocalBusiness.
8
 * Allows the bulk setup of properties
9
 */
10
class LocalBusiness extends AbstractModel implements \BOTK\ModelInterface 
11
{
12
13
	protected static $DEFAULT_OPTIONS = array (
14
		'businessType'		=> array(		
15
								// additional types  as extension of schema:LocalBusiness
16
			'filter'    => FILTER_DEFAULT,
17
			'flags'  	=> FILTER_FORCE_ARRAY
18
			),
19
		'taxID'				=> array(	
20
			'filter'    => FILTER_CALLBACK,
21
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_TOKEN',
22
			'flags'  	=> FILTER_REQUIRE_SCALAR
23
			),
24
		'vatID'				=> array(	// italian rules
25
			'filter'    => FILTER_VALIDATE_REGEXP,
26
			'options' 	=> array('regexp'=>'/^[0-9]{11}$/'),
27
			'flags'  	=> FILTER_REQUIRE_SCALAR
28
			),
29
		'legalName'			=> array(
30
			'filter'    => FILTER_CALLBACK,
31
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
32
			'flags'  	=> FILTER_REQUIRE_SCALAR
33
			),
34
		'businessName'		=> array(
35
								// a schema:alternateName for schema:PostalAddress
36
			'filter'    => FILTER_DEFAULT,
37
			'flags'  	=> FILTER_FORCE_ARRAY
38
			),
39
		'addressDescription'=> array(	//	
40
			'filter'    => FILTER_CALLBACK,
41
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
42
			'flags'  	=> FILTER_REQUIRE_SCALAR
43
			),
44
		'addressCountry'	=> array(
45
			'default'	=> 'IT',		
46
			'filter'    => FILTER_VALIDATE_REGEXP,
47
			'options' 	=> array('regexp'=>'/^[A-Z]{2}$/'),
48
			'flags'  	=> FILTER_REQUIRE_SCALAR
49
			),
50
		'addressLocality'	=> array(	
51
			'filter'    => FILTER_CALLBACK,
52
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
53
			'flags'  	=> FILTER_REQUIRE_SCALAR
54
			),
55
		'addressRegion'		=> array(	
56
			'filter'    => FILTER_CALLBACK,
57
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
58
			'flags'  	=> FILTER_REQUIRE_SCALAR
59
			),
60
		'streetAddress'		=> array(	
61
			'filter'    => FILTER_CALLBACK,
62
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
63
			'flags'  	=> FILTER_REQUIRE_SCALAR
64
			),
65
		'postalCode'		=> array(	// italian rules
66
			'filter'    => FILTER_VALIDATE_REGEXP,
67
			'options' 	=> array('regexp'=>'/^[0-9]{5}$/'),
68
			'flags'  	=> FILTER_REQUIRE_SCALAR
69
			),
70
		'telephone'			=> array(	
71
			'filter'    => FILTER_CALLBACK,	
72
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_TELEPHONE',
73
			'flags'  	=> FILTER_FORCE_ARRAY
74
			),
75
		'faxNumber'			=> array(	
76
			'filter'    => FILTER_CALLBACK,
77
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_TELEPHONE',
78
			'flags'  	=> FILTER_FORCE_ARRAY
79
			),
80
		'email'				=> array(	
81
			'filter'    => FILTER_CALLBACK,
82
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_EMAIL',
83
			'flags'  	=> FILTER_FORCE_ARRAY
84
			),
85
		'lat'				=> array( 
86
			'filter'    => FILTER_CALLBACK,
87
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_GEO'
88
			),
89
		'long'				=> array( 
90
			'filter'    => FILTER_CALLBACK,
91
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_GEO'
92
			),
93
		'similarStreet'		=> array(	
94
			'filter'    => FILTER_CALLBACK,
95
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
96
			'flags'  	=> FILTER_FORCE_ARRAY
97
			),
98
		'hasMap'			=> array(	
99
			'filter'    => FILTER_CALLBACK,
100
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_HTTP_URL',
101
			'flags'  	=> FILTER_FORCE_ARRAY
102
			),
103
		'aggregateRatingValue'	=> array(	
104
			'filter'    => FILTER_VALIDATE_FLOAT,
105
			'flags'  	=> FILTER_REQUIRE_SCALAR
106
			),
107
		'openingHours'		   => array(	
108
			'filter'    => FILTER_DEFAULT,
109
			'flags'  	=> FILTER_REQUIRE_SCALAR
110
			),
111
		'near'				=> array(	
112
			'filter'    => FILTER_CALLBACK,
113
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
114
			'flags'  	=> FILTER_FORCE_ARRAY
115
			),
116
		'similarName'		=> array(	
117
			'filter'    => FILTER_CALLBACK,
118
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
119
			'flags'  	=> FILTER_FORCE_ARRAY
120
			),		
121
		'numberOfEmployees'	  => array(	
122
			'filter'    => FILTER_VALIDATE_REGEXP,
123
			'options' 	=> array('regexp'=>'/^[0-9]+\s*-?\s*[0-9]*$/'),
124
			'flags'  	=> FILTER_REQUIRE_SCALAR
125
			),
126
		'annualTurnover'	 => array(	
127
			'filter'    => FILTER_VALIDATE_REGEXP,
128
			'options' 	=> array('regexp'=>'/^-?[0-9]+\s*-?\s*-?[0-9]*$/'),
129
			'flags'  	=> FILTER_REQUIRE_SCALAR
130
			),
131
		'ateco2007'			=> array(	
132
			'filter'    => FILTER_VALIDATE_REGEXP,
133
			'options' 	=> array('regexp'=>'/^[0-9]{6}$/'),
134
			'flags'  	=> FILTER_REQUIRE_SCALAR
135
			),
136
		'ebitda'			=> array(	
137
			'filter'    => FILTER_VALIDATE_REGEXP,
138
			'options' 	=> array('regexp'=>'/^-?[0-9]+\s*-?\s*-?[0-9]*$/'),
139
			'flags'  	=> FILTER_REQUIRE_SCALAR
140
			),
141
		'netProfit'			=> array(	
142
			'filter'    => FILTER_VALIDATE_REGEXP,
143
			'options' 	=> array('regexp'=>'/^-?[0-9]+\s*-?\s*-?[0-9]*$/'),
144
			'flags'  	=> FILTER_REQUIRE_SCALAR
145
			),
146
		'naceV2'			=> array(	
147
			'filter'    => FILTER_VALIDATE_REGEXP,
148
			'options' 	=> array('regexp'=>'/^[0-9]{2}[.]?[0-9]{2}[.]?[0-9]{2}$/'),
149
			'flags'  	=> FILTER_REQUIRE_SCALAR
150
			),
151
		'itBudget'	=> array(	
152
			'filter'    => FILTER_VALIDATE_REGEXP,
153
			'options' 	=> array('regexp'=>'/^-?[0-9]+\s*-?\s*-?[0-9]*$/'),
154
			'flags'  	=> FILTER_REQUIRE_SCALAR
155
			),
156
		'itStorageBudget'	=> array(	
157
			'filter'    => FILTER_VALIDATE_REGEXP,
158
			'options' 	=> array('regexp'=>'/^-?[0-9]+\s*-?\s*-?[0-9]*$/'),
159
			'flags'  	=> FILTER_REQUIRE_SCALAR
160
			),
161
		'itHardwareBudget'	=> array(	
162
			'filter'    => FILTER_VALIDATE_REGEXP,
163
			'options' 	=> array('regexp'=>'/^-?[0-9]+\s*-?\s*-?[0-9]*$/'),
164
			'flags'  	=> FILTER_REQUIRE_SCALAR
165
			),
166
		'itServerBudget'	=> array(	
167
			'filter'    => FILTER_VALIDATE_REGEXP,
168
			'options' 	=> array('regexp'=>'/^-?[0-9]+\s*-?\s*-?[0-9]*$/'),
169
			'flags'  	=> FILTER_REQUIRE_SCALAR
170
			),
171
		'softwareBudget'	=> array(	
172
			'filter'    => FILTER_VALIDATE_REGEXP,
173
			'options' 	=> array('regexp'=>'/^-?[0-9]+\s*-?\s*-?[0-9]*$/'),
174
			'flags'  	=> FILTER_REQUIRE_SCALAR
175
			)
176
		);
177
178
	/**
179
	 * Redefine protected constructor to add address description as dynamic property
180
	 */
181 13
	protected function __construct(array $data = array(), array $customOptions = array()) 
182
	{
183 13
		parent::__construct($data, $customOptions);
184 13
		$this->addAddressDescription();
185 13
	}
186
	
187
	
188
	/**
189
	 * If not existing, create an address description as a normalized address from following data properties:
190
	 * 		'addressLocality',
191
	 * 		'addressRegion',
192
	 * 		'streetAddress',
193
	 * 		'postalCode',
194
	 */
195 13
	private function addAddressDescription()
196
	{	
197 13
		extract($this->data);
198
199 13
		if(empty($addressDescription)){
200 10
			if( !empty($streetAddress) && ( !empty($addressLocality) || !empty($postalCode))){
201 4
				$addressDescription = "$streetAddress ,";
202 4
				if(!empty($postalCode)) { $addressDescription.= " $postalCode";}
203 4
				if(!empty($addressLocality)) { $addressDescription.= " $addressLocality"; }
204 4
				if(!empty($addressRegion)) { $addressDescription.= " ($addressRegion)"; }
205
			} else {
206 6
				$addressDescription = null;
207
			}
208
		}
209
		
210 13
		$addressDescription = \BOTK\Filters::FILTER_SANITIZE_ADDRESS($addressDescription);
211
		
212 13
		if(!empty($addressDescription)){
213 7
			$this->data['addressDescription']=$addressDescription;
214
		}
215 13
	}
216
	
217
	
218 4
	public function asTurtleFragment()
219
	{
220 4
		if(is_null($this->rdf)) {
221 4
			extract($this->data);
222
223
			//die(print_r($this->data, true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
224
225
			// create uris
226 4
			$organizationUri = $this->getUri();
227 4
			$addressUri = $organizationUri.'_address';
228 4
			$geoUri = ( !empty($lat) && !empty($long) )?"geo:$lat,$long":null;
229
			
230 4
			$tripleCounter =0;
231 4
			$turtleString='';
232
			
233
			// define $_ as a macro to write simple rdf
234 4
			$_= function($format, $var,$sanitize=true) use(&$turtleString, &$tripleCounter){
235 4
				foreach((array)$var as $v){
236 4
					if($var){
237 4
						$turtleString.= sprintf($format,$sanitize?\BOTK\Filters::FILTER_SANITIZE_TURTLE_STRING($v):$v);
238 4
						$tripleCounter++;
239
					}
240
				}
241 4
			};
242
243
	 		// serialize schema:LocalBusiness
244
			$_('<%s> a schema:LocalBusiness;', $organizationUri);
245
			!empty($businessType) 		&& $_('a %s;', $businessType);
246
			!empty($id) 				&& $_('dct:identifier "%s";', $id);
247
			!empty($vatID) 				&& $_('schema:vatID "%s";', $vatID); 
248
			!empty($taxtID) 			&& $_('schema:taxtID "%s";', $taxID);
249
			!empty($legalName)			&& $_('schema:legalName "%s";', $legalName);
250
			!empty($businessName) 		&& $_('schema:alternateName "%s";', $businessName);
251
			!empty($telephone) 			&& $_('schema:telephone "%s";', $telephone);
252
			!empty($faxNumber) 			&& $_('schema:faxNumber "%s";', $faxNumber);
253
			!empty($openingHours)		&& $_('schema:openingHours "%s";', $openingHours);
254
			!empty($disambiguatingDescription)&& $_('schema:disambiguatingDescription "%s";', $disambiguatingDescription);
255
			!empty($ateco2007)			&& $_('botk:ateco2007 "%s";', $ateco2007);
256
			!empty($naceV2)				&& $_('botk:nace "%s";', $naceV2);
257
			!empty($aggregateRatingValue)&& $_('schema:aggregateRating [a schema:AggregateRating; schema:ratingValue "%s"^^xsd:float];', $aggregateRatingValue);
258
			!empty($page) 				&& $_('foaf:page <%s>;', $page,false);
259
			!empty($email) 				&& $_('schema:email "%s";', $email);
260
			!empty($homepage) 			&& $_('foaf:homepage <%s>;', $homepage,false);
261
			!empty($geoUri) 			&& $_('schema:geo <%s>;', $geoUri,false);
262
			!empty($hasMap) 			&& $_('schema:hasMap <%s>;', $hasMap,false);
263
			$_('schema:address <%s>. ', $addressUri);
264
			
265
			// serialize schema:PostalAddress 
266
			$_('<%s> a schema:PostalAddress;', $addressUri);
267
			!empty($addressDescription) && $_('schema:description "%s";', $addressDescription);
268
			!empty($streetAddress) 		&& $_('schema:streetAddress "%s";', $streetAddress);
269
			!empty($postalCode) 		&& $_('schema:postalCode "%s";', $postalCode);
270
			!empty($addressLocality) 	&& $_('schema:addressLocality "%s";', $addressLocality);
271
			!empty($addressRegion) 		&& $_('schema:addressRegion "%s";', $addressRegion);			
272
			$_('schema:addressCountry "%s". ', $addressCountry);
273
274
			// serialize schema:GeoCoordinates
275
			if( !empty($geoUri)){
276
				$_('<%s> a schema:GeoCoordinates;', $geoUri); 
277
				$_('wgs:lat "%s"^^xsd:float;', $lat);
278
				$_('wgs:long "%s"^^xsd:float . ', $long); 
279
			}
280
			
281
			$statVars = array(
282
				'numberOfEmployees',
283
				'annualTurnover',
284
				'ebitda',
285
				'netProfit',
286
				'itBudget',
287
				'itStorageBudget',
288
				'itHardwareBudget',
289
				'itServerBudget',
290
				'softwareBudget'
291
				);
292
			
293
			foreach ( $statVars as $statVar){
294
				if(!empty($this->data[$statVar]) && preg_match('/^(-?[0-9]+)\s*-?\s*(-?[0-9]*)$/', $this->data[$statVar], $matches)){
295
					$statUri =  $organizationUri.'_'.$statVar;
296
297
					$_("<$organizationUri> botk:$statVar <%s> .", $statUri, false);	
298
299
					$_('<%s> a schema:QuantitativeValue, botk:EstimatedRange;', $statUri);
300
					$minValue =  (int) $matches[1];
301
					$maxValue = empty($matches[2])? $minValue : (int) $matches[2];
302
					$_('schema:minValue %s ;', $minValue);
303
					$_('schema:maxValue %s .', $maxValue);
304
				}		
305
			}
306
307
			$this->rdf = $turtleString;
308
			$this->tripleCount = $tripleCounter;
309
		}
310
311
		return $this->rdf;
312
	}
313
314
}