Passed
Push — master ( 85416d...296d2d )
by
unknown
04:05
created

LocalBusiness::asTurtleFragment()   F

Complexity

Conditions 33
Paths > 20000

Size

Total Lines 90
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 34.3378

Importance

Changes 0
Metric Value
dl 0
loc 90
ccs 50
cts 56
cp 0.8929
rs 2.1214
c 0
b 0
f 0
cc 33
eloc 56
nc 16777217
nop 0
crap 34.3378

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
127
		);
128
129
	/**
130
	 * Redefine protected constructor to add address description as dynamic property
131
	 */
132 13
	protected function __construct(array $data = array(), array $customOptions = array()) 
133
	{
134 13
		parent::__construct($data, $customOptions);
135 13
		$this->addAddressDescription();
136 13
	}
137
	
138
	
139
	/**
140
	 * If not existing, create an address description as a normalized address from following data properties:
141
	 * 		'addressLocality',
142
	 * 		'addressRegion',
143
	 * 		'streetAddress',
144
	 * 		'postalCode',
145
	 */
146 13
	private function addAddressDescription()
147
	{	
148 13
		extract($this->data);
149
150 13
		if(empty($addressDescription)){
151 10
			if( !empty($streetAddress) && ( !empty($addressLocality) || !empty($postalCode))){
152 4
				$addressDescription = "$streetAddress ,";
153 4
				if(!empty($postalCode)) { $addressDescription.= " $postalCode";}
154 4
				if(!empty($addressLocality)) { $addressDescription.= " $addressLocality"; }
155 4
				if(!empty($addressRegion)) { $addressDescription.= " ($addressRegion)"; }
156
			} else {
157 6
				$addressDescription = null;
158
			}
159
		}
160
		
161 13
		$addressDescription = \BOTK\Filters::FILTER_SANITIZE_ADDRESS($addressDescription);
162
		
163 13
		if(!empty($addressDescription)){
164 7
			$this->data['addressDescription']=$addressDescription;
165
		}
166 13
	}
167
	
168
	
169 4
	public function asTurtleFragment()
170
	{
171 4
		if(is_null($this->rdf)) {
172 4
			extract($this->data);
173
174
			// create uris
175 4
			$organizationUri = $this->getUri();
176 4
			$addressUri = $organizationUri.'_address';
177 4
			$geoUri = ( !empty($lat) && !empty($long) )?"geo:$lat,$long":null;
178
			$statVars = array(
179
				'numberOfEmployees'
180 4
			);
181
			
182 4
			$tripleCounter =0;
183 4
			$turtleString='';
184
			
185
			// define $_ as a macro to write simple rdf
186 4
			$_= function($format, $var,$sanitize=true) use(&$turtleString, &$tripleCounter){
187 4
				foreach((array)$var as $v){
188 4
					if($var){
189 4
						$turtleString.= sprintf($format,$sanitize?\BOTK\Filters::FILTER_SANITIZE_TURTLE_STRING($v):$v);
190 4
						$tripleCounter++;
191
					}
192
				}
193 4
			};
194
195
	 		// serialize schema:LocalBusiness
196 4
			$_('<%s> a schema:LocalBusiness;', $organizationUri);
197 4
			!empty($businessType) 		&& $_('a %s;', $businessType);
198 4
			!empty($id) 				&& $_('dct:identifier "%s";', $id);
199 4
			!empty($vatID) 				&& $_('schema:vatID "%s";', $vatID); 
200 4
			!empty($taxtID) 			&& $_('schema:taxtID "%s";', $taxID);
201 4
			!empty($legalName)			&& $_('schema:legalName "%s";', $legalName);
202 4
			!empty($businessName) 		&& $_('schema:alternateName "%s";', $businessName);
203 4
			!empty($telephone) 			&& $_('schema:telephone "%s";', $telephone);
204 4
			!empty($faxNumber) 			&& $_('schema:faxNumber "%s";', $faxNumber);
205 4
			!empty($openingHours)		&& $_('schema:openingHours "%s";', $openingHours);
206 4
			!empty($disambiguatingDescription)&& $_('schema:disambiguatingDescription "%s";', $disambiguatingDescription);
207 4
			!empty($aggregateRatingValue)&& $_('schema:aggregateRating [a schema:AggregateRating; schema:ratingValue "%s"^^xsd:float];', $aggregateRatingValue);
208 4
			!empty($page) 				&& $_('foaf:page <%s>;', $page,false);
209 4
			!empty($email) 				&& $_('schema:email "%s";', $email);
210 4
			!empty($homepage) 			&& $_('foaf:homepage <%s>;', $homepage,false);
211 4
			!empty($geoUri) 			&& $_('schema:geo <%s>;', $geoUri,false);
212 4
			!empty($hasMap) 			&& $_('schema:hasMap <%s>;', $hasMap,false);
213 4
			$_('schema:address <%s>. ', $addressUri);
214
			
215
			// serialize schema:PostalAddress 
216 4
			$_('<%s> a schema:PostalAddress;', $addressUri);
217 4
			!empty($addressDescription) && $_('schema:description "%s";', $addressDescription);
218 4
			!empty($streetAddress) 		&& $_('schema:streetAddress "%s";', $streetAddress);
219 4
			!empty($postalCode) 		&& $_('schema:postalCode "%s";', $postalCode);
220 4
			!empty($addressLocality) 	&& $_('schema:addressLocality "%s";', $addressLocality);
221 4
			!empty($addressRegion) 		&& $_('schema:addressRegion "%s";', $addressRegion);			
222 4
			$_('schema:addressCountry "%s". ', $addressCountry);
223
224
			// serialize schema:GeoCoordinates
225 4
			if( !empty($geoUri)){
226 2
				$_('<%s> a schema:GeoCoordinates;', $geoUri); 
227 2
				$_('wgs:lat "%s"^^xsd:float;', $lat);
228 2
				$_('wgs:long "%s"^^xsd:float . ', $long); 
229
			}
230
			
231
			
232
233 4
			foreach ( $statVars as $statVar){
234 4
				if(!empty($this->data[$statVar]) && preg_match('/^([0-9]+)\s*-?\s*([0-9])*$/', $this->data[$statVar], $matches)){
235
					$statUri =  $organizationUri.'_'.$statVar;
236
				    $_("<$organizationUri> botk:$statVar <%s> .", $statUri, false);	
237
					$_('<%s> a schema:QuantitativeValue, botk:EstimatedRange;', $statUri);
238
					$minValue =  (int) $matches[1];
239
					$maxValue = empty($matches[2])? $minValue : (int) $matches[2];
240
					$_('schema:minValue %s ;', $minValue);
241 4
					$_('schema:maxValue %s .', $maxValue);
242
				}		
243
			}
244
			/*if(!empty($numberOfEmployees) 	&& 	preg_match('/^([0-9]+)\s*-?\s*([0-9])*$/', $numberOfEmployees, $matches)){	
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% 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...
245
246
				$_('<%s> a schema:QuantitativeValue, botk:EstimatedRange;', $numberOfEmployeesUri);
247
				$minValue =  (int) $matches[1];
248
				$maxValue = empty($matches[2])? $minValue : (int) $matches[2];
249
				$_('schema:minValue %s ;', $minValue);
250
				$_('schema:maxValue %s .', $maxValue);
251
			}*/
252
253 4
			$this->rdf = $turtleString;
254 4
			$this->tripleCount = $tripleCounter;
255
		}
256
257 4
		return $this->rdf;
258
	}
259
260
}