Test Setup Failed
Branch botk5 (03a709)
by Enrico
02:14
created

LocalBusiness   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 47
lcom 1
cbo 2
dl 0
loc 172
c 0
b 0
f 0
rs 8.439

1 Method

Rating   Name   Duplication   Size   Complexity  
F asTurtle() 0 86 47

How to fix   Complexity   

Complex Class

Complex classes like LocalBusiness often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use LocalBusiness, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace BOTK\Model;
3
4
use BOTK\Exceptions\DataModelException;
5
use BOTK\ModelInterface;
6
7
/**
8
 * An ibrid class that merge the semantic of schema:organization, schema:place and schema:geo, 
9
 * it is similar to schema:LocalBusiness.
10
 * Allows the bulk setup of properties
11
 */
12
class LocalBusiness extends AbstractModel implements ModelInterface 
13
{
14
15
	static protected $DEFAULT_OPTIONS = array (
16
		'base'				=> array(
17
								'default'	=> 'http://linkeddata.center/botk/resource/',
18
								'filter'    => FILTER_SANITIZE_URL,
19
			                   ),
20
		'uri'				=> array(
21
								'filter'    => FILTER_SANITIZE_URL,
22
			                   ),
23
		'lang'				=> array(
24
								'default'	=> 'it',		
25
								'filter'    => FILTER_VALIDATE_REGEXP,
26
		                        'options' 	=> array('regexp'=>'/^[a-z]{2}$/')
27
			                   ),
28
		'id'				=> array(		
29
								'filter'    => FILTER_VALIDATE_REGEXP,
30
		                        'options' 	=> array('regexp'=>'/^[\w]+$/')
31
			                   ),
32
		'taxID'				=> array(	
33
								'filter'    => FILTER_CALLBACK,
34
		                        'options' 	=> '\BOTK\Filters::normalizeToken'
35
			                   ),
36
		'vatID'				=> array(	// italian rules
37
								'filter'    => FILTER_VALIDATE_REGEXP,
38
		                        'options' 	=> array('regexp'=>'/^[0-9]{11}$/')
39
			                   ),
40
		'legalName'			=> array(
41
								'filter'    => FILTER_CALLBACK,
42
		                        'options' 	=> '\BOTK\Filters::normalizeAddress'
43
			                   ),
44
		'alternateName'		=> array(),
45
		'addressCountry'	=> array(
46
								'default'	=> 'IT',		
47
								'filter'    => FILTER_VALIDATE_REGEXP,
48
		                        'options' 	=> array('regexp'=>'/^[A-Z]{2}$/')
49
			                   ),
50
		'addressLocality'	=> array(	
51
								'filter'    => FILTER_CALLBACK,
52
		                        'options' 	=> '\BOTK\Filters::normalizeAddress'
53
			                   ),
54
		'addressRegion'		=> array(	
55
								'filter'    => FILTER_CALLBACK,
56
		                        'options' 	=> '\BOTK\Filters::normalizeAddress'
57
			                   ),
58
		'streetAddress'		=> array(	
59
								'filter'    => FILTER_CALLBACK,
60
		                        'options' 	=> '\BOTK\Filters::normalizeAddress'
61
			                   ),
62
		'postalCode'		=> array(	// italian rules
63
								'filter'    => FILTER_VALIDATE_REGEXP,
64
		                        'options' 	=> array('regexp'=>'/^[0-9]{5}$/')
65
			                   ),
66
		'page'				=> array(	
67
								'filter'    => FILTER_SANITIZE_URL
68
			                   ),
69
		'telephone'			=> array(	
70
								'filter'    => FILTER_CALLBACK,
71
		                        'options' 	=> '\BOTK\Filters::normalizeItTelephone'
72
			                   ),
73
		'faxNumber'			=> array(	
74
								'filter'    => FILTER_CALLBACK,
75
		                        'options' 	=> '\BOTK\Filters::normalizeItTelephone'
76
			                   ),
77
		'email'				=> array(	
78
								'filter'    => FILTER_CALLBACK,
79
		                        'options' 	=> '\BOTK\Filters::normalizeEmail'
80
			                   ),
81
		'geoDescription'	=> array(	
82
								'filter'    => FILTER_CALLBACK,
83
		                        'options' 	=> '\BOTK\Filters::normalizeAddress'
84
			                   ),
85
		'lat'				=> array( // http://www.regexlib.com/REDetails.aspx?regexp_id=2728
86
								'filter'    => FILTER_VALIDATE_REGEXP,
87
		                        'options' 	=> array('regexp'=>'/^-?([1-8]?[0-9]\.{1}\d{1,6}$|90\.{1}0{1,6}$)/')
88
			                   ),
89
		'long'				=> array( // http://stackoverflow.com/questions/3518504/regular-expression-for-matching-latitude-longitude-coordinates
90
								'filter'    => FILTER_VALIDATE_REGEXP,
91
		                        'options' 	=> array('regexp'=>'/-?([1-8]?[0-9]\.{1}\d{1,6}$|90\.{1}0{1,6}$)/')
92
			                   ),
93
	);
94
	
95
	
96
	public function asTurtle()
97
	{
98
		if(is_null($this->rdf)) {
99
			$tc =0;
100
			$rdf='';
101
			extract($this->data);
102
103
			// create uris
104
			$organizationUri = empty($uri)?($base. (empty($id)?uniqid():$id)):$uri;
105
			$addressUri = $organizationUri.'_address';
106
			$placeUri = $organizationUri.'_place';
107
			$geoUri = ( !empty($lat) && !empty($long) )?"geo:$lat,$long":($organizationUri.'_geo'); 
108
109
			
110
			// define the minimum condition to skipp the rdf generation
111
			
112
			$skippAddress = 	empty($alternateName) &&
113
								empty($addressLocality) &&
114
								empty($streetAddress) &&
115
								empty($postalCode) &&
116
								empty($page) &&
117
								empty($telephone) &&
118
								empty($faxNumber) &&
119
								empty($email)
120
			;
121
			$skippGeo = empty($geoDescription) &&
122
						empty($addressLocality) &&
123
						empty($streetAddress) &&
124
						empty($lat) && 
125
						empty($long) ;
126
			$skippPlace = $skippGeo && $skippAddress;
127
			
128
			$skippOrganization = $skippPlace && empty($id)&& empty($vatID) && empty($taxID) && empty($legalName) ;
129
130
	 		// serialize schema:Organization
131
	 		if( !$skippOrganization){
132
				$rdf.= sprintf( '<%s> a schema:Organization;', $organizationUri); $tc++;
133
				if(!empty($id)) { $rdf.= sprintf( 'dct:identifier "%s";', $id); $tc++; }
134
				if(!empty($vatID)) { $rdf.= sprintf( 'schema:vatID "%s"@%s;', $vatID, $lang); $tc++; }
135
				if(!empty($taxtID)) { $rdf.= sprintf( 'schema:taxtID "%s"@%s;', $taxID, $lang); $tc++; }
136
				if(!empty($legalName)) { $rdf.= sprintf( 'schema:legalName """%s"""@%s;', $legalName, $lang); $tc++; }
137
				if( !$skippPlace) { $rdf.= sprintf( 'schema:location <%s>;', $placeUri); $tc++; }
138
				$rdf.= " . ";
139
			}	
140
			
141
			// serialize schema:PostalAddress 
142
			if( !$skippAddress){
143
				$rdf.= sprintf( '<%s> a schema:PostalAddress;', $addressUri); $tc++;
144
				if(!empty($alternateName)) { $rdf.= sprintf( 'schema:alternateName """%s"""@%;', $addressCountry,$lang); $tc++; }
145
				if(!empty($streetAddress)) { $rdf.= sprintf( 'schema:streetAddress """%s"""@%s;', $streetAddress, $lang); $tc++; }
146
				if(!empty($postalCode)) { $rdf.= sprintf( 'schema:postalCode "%s"@%s;', $postalCode, $lang); $tc++; }
147
				if(!empty($addressLocality)) { $rdf.= sprintf( 'schema:addressLocality """%s"""@%s;', $addressLocality, $lang); $tc++; }
148
				if(!empty($addressRegion)) { $rdf.= sprintf( 'schema:addressRegion """%s"""@%s;', $addressRegion, $lang); $tc++; }
149
				if(!empty($addressCountry)) { $rdf.= sprintf( 'schema:addressCountry "%s";', $addressCountry); $tc++; }
150
				if(!empty($telephone)) { $rdf.= sprintf( 'schema:telephone """%s"""@%s;', $telephone, $lang); $tc++; }
151
				if(!empty($faxNumber)) { $rdf.= sprintf( 'schema:faxNumber """%s"""@%s;', $faxNumber, $lang); $tc++; }
152
				if(!empty($page)) { $rdf.= sprintf( 'schema:page <%s>;', $page); $tc++; }
153
				if(!empty($email)) { $rdf.= sprintf( 'schema:email "%s";', $email); $tc++; }
154
				$rdf.= " . ";
155
			}
156
157
			// serialize schema:GeoCoordinates
158
			if( !$skippGeo){
159
				$geoLabel = \BOTK\Filters::buildNormalizedAddress($this->data);
160
				$rdf.= sprintf( '<%s> a schema:GeoCoordinates;', $geoUri); $tc++;
161
				if(!empty($geoLabel)) { $rdf.= sprintf( 'schema:alternateLabel ""%s""@%s;', $geoLabel, $lang); $tc++; }
162
				if(!empty($geoDescription)) { $rdf.= sprintf( 'schema:alternateLabel ""%s""@%s;', $geoDescription, $lang); $tc++; }
163
				if(!empty($lat)) { $rdf.= sprintf( 'wgs:lat %s ;', $lat); $tc++; }
164
				if(!empty($long)) { $rdf.= sprintf( 'wgs:long %s ;', $long); $tc++; }
165
				$rdf.= " . ";	
166
			}
167
168
			// serialize schema:Place
169
			if( !$skippPlace){
170
				$rdf.= sprintf( '<%s> a schema:LocalBusiness;', $placeUri); $tc++;
171
				if(!$skippAddress) { $rdf.= sprintf( 'schema:address <%s>;', $addressUri); $tc++; }
172
				if(!$skippGeo) { $rdf.= sprintf( 'schema:geo <%s>;', $geoUri); $tc++; }
173
				$rdf.= " . ";
174
			}
175
	
176
			$this->rdf = $rdf;
177
			$this->tripleCount  = $tc;
178
		}
179
		
180
		return $this->rdf;
181
	}
182
	
183
}