Source   B
last analyzed

Complexity

Total Complexity 46

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 243
rs 8.72
c 0
b 0
f 0
wmc 46
lcom 1
cbo 1

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A getAllLocationInfo() 0 12 2
A getLocationInfobyName() 0 12 2
A getLocationInfobyNameType() 0 12 2
A getLocationInfobySourceName() 0 13 2
B getLocationInfoByType() 0 21 6
A getLocationInfoByLocationID() 0 12 2
A getLocationInfoByID() 0 12 2
A addLocation() 0 11 3
A updateLocation() 0 12 3
A updateLocationDescByName() 0 11 2
A updateLocationByLocationID() 0 11 3
A deleteLocation() 0 11 2
A deleteLocationByType() 0 11 2
A deleteLocationBySource() 0 11 2
A deleteAllLocation() 0 10 2
B deleteOldLocationByType() 0 29 7

How to fix   Complexity   

Complex Class

Complex classes like Source 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 Source, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * This class is part of FlightAirmap. It's used to set and get sources (and weather stations) info
4
 *
5
 * Copyright (c) Ycarus (Yannick Chabanois) at Zugaina <[email protected]>
6
 * Licensed under AGPL license.
7
 * For more information see: https://www.flightairmap.com/
8
*/
9
require_once(dirname(__FILE__).'/settings.php');
10
require_once(dirname(__FILE__).'/class.Connection.php');
11
12
class Source {
13
	public $db;
14
15
	/*
16
	 * Initialize DB connection
17
	*/
18
	public function __construct($dbc = null) {
19
		$Connection = new Connection($dbc);
20
		$this->db = $Connection->db;
21
		if ($this->db === null) die('Error: No DB connection. (Source)');
22
	}
23
24
	public function getAllLocationInfo() {
25
		$query = "SELECT * FROM source_location";
26
		$query_values = array();
27
		try {
28
			$sth = $this->db->prepare($query);
29
			$sth->execute($query_values);
30
		} catch(PDOException $e) {
31
			return "error : ".$e->getMessage();
32
		}
33
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
34
		return $all;
35
	}
36
37
	public function getLocationInfobyName($name) {
38
		$query = "SELECT * FROM source_location WHERE name = :name";
39
		$query_values = array(':name' => $name);
40
		try {
41
			$sth = $this->db->prepare($query);
42
			$sth->execute($query_values);
43
		} catch(PDOException $e) {
44
			return "error : ".$e->getMessage();
45
		}
46
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
47
		return $all;
48
	}
49
50
	public function getLocationInfobyNameType($name,$type) {
51
		$query = "SELECT * FROM source_location WHERE name = :name AND type = :type";
52
		$query_values = array(':name' => $name,':type' => $type);
53
		try {
54
			$sth = $this->db->prepare($query);
55
			$sth->execute($query_values);
56
		} catch(PDOException $e) {
57
			return "error : ".$e->getMessage();
58
		}
59
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
60
		return $all;
61
	}
62
63
    /**
64
     * @param $name
65
     * @return array
66
     */
67
    public function getLocationInfobySourceName($name) {
68
		$query = "SELECT * FROM source_location WHERE source = :name";
69
		$query_values = array(':name' => $name);
70
		try {
71
			$sth = $this->db->prepare($query);
72
			$sth->execute($query_values);
73
		} catch(PDOException $e) {
74
			echo "error : ".$e->getMessage();
75
			return array();
76
		}
77
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
78
		return $all;
79
	}
80
81
	public function getLocationInfoByType($type, $coord = array(), $limit = false) {
82
		$query = "SELECT * FROM source_location WHERE type = :type";
83
		if (is_array($coord) && !empty($coord) && count($coord) == 4) {
84
			$minlong = filter_var($coord[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
85
			$minlat = filter_var($coord[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
86
			$maxlong = filter_var($coord[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
87
			$maxlat = filter_var($coord[3],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
88
			$query .= " AND source_location.latitude BETWEEN ".$minlat." AND ".$maxlat." AND source_location.longitude BETWEEN ".$minlong." AND ".$maxlong." AND source_location.latitude <> 0 AND source_location.longitude <> 0";
89
		}
90
		$query .= " ORDER BY last_seen DESC";
91
		if ($limit) $query .= " LIMIT 1000";
92
		$query_values = array(':type' => $type);
93
		try {
94
			$sth = $this->db->prepare($query);
95
			$sth->execute($query_values);
96
		} catch(PDOException $e) {
97
			return "error : ".$e->getMessage();
98
		}
99
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
100
		return $all;
101
	}
102
103
	public function getLocationInfoByLocationID($location_id) {
104
		$query = "SELECT * FROM source_location WHERE location_id = :location_id";
105
		$query_values = array(':location_id' => $location_id);
106
		try {
107
			$sth = $this->db->prepare($query);
108
			$sth->execute($query_values);
109
		} catch(PDOException $e) {
110
			return "error : ".$e->getMessage();
111
		}
112
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
113
		return $all;
114
	}
115
116
	public function getLocationInfoByID($id) {
117
		$query = "SELECT * FROM source_location WHERE id = :id";
118
		$query_values = array(':id' => $id);
119
		try {
120
			$sth = $this->db->prepare($query);
121
			$sth->execute($query_values);
122
		} catch(PDOException $e) {
123
			return "error : ".$e->getMessage();
124
		}
125
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
126
		return $all;
127
	}
128
129
	public function addLocation($name,$latitude,$longitude,$altitude,$city,$country,$source,$logo = 'antenna.png',$type = '',$source_id = 0,$location_id = 0,$last_seen = '', $description = '') {
130
		if ($last_seen == '') $last_seen = date('Y-m-d H:i:s');
131
		$query = "INSERT INTO source_location (name,latitude,longitude,altitude,country,city,logo,source,type,source_id,last_seen,location_id,description) VALUES (:name,:latitude,:longitude,:altitude,:country,:city,:logo,:source,:type,:source_id,:last_seen,:location_id,:description)";
132
		$query_values = array(':name' => $name,':latitude' => $latitude, ':longitude' => $longitude,':altitude' => $altitude,':city' => $city,':country' => $country,':logo' => $logo,':source' => $source,':type' => $type,':source_id' => $source_id,':last_seen' => $last_seen,':location_id' => $location_id,':description' => $description);
133
		try {
134
			$sth = $this->db->prepare($query);
135
			$sth->execute($query_values);
136
		} catch(PDOException $e) {
137
			echo "error : ".$e->getMessage();
138
		}
139
	}
140
141
	public function updateLocation($name,$latitude,$longitude,$altitude,$city,$country,$source,$logo = 'antenna.png',$type = '',$source_id = 0,$location_id = 0,$last_seen = '',$description = '') {
142
		if ($last_seen == '') $last_seen = date('Y-m-d H:i:s');
143
		$query = "UPDATE source_location SET latitude = :latitude,longitude = :longitude,altitude = :altitude,country = :country,city = :city,logo = :logo,type = :type, source_id = :source_id, last_seen = :last_seen,location_id = :location_id, description = :description WHERE name = :name AND source = :source";
144
		$query_values = array(':name' => $name,':latitude' => $latitude, ':longitude' => $longitude,':altitude' => $altitude,':city' => $city,':country' => $country,':logo' => $logo,':source' => $source,':type' => $type,':source_id' => $source_id,':last_seen' => $last_seen,':location_id' => $location_id,':description' => $description);
145
		try {
146
			$sth = $this->db->prepare($query);
147
			$sth->execute($query_values);
148
		} catch(PDOException $e) {
149
			return "error : ".$e->getMessage();
150
		}
151
		return '';
152
	}
153
154
	public function updateLocationDescByName($name,$source,$source_id = 0,$description = '') {
155
		$query = "UPDATE source_location SET description = :description WHERE source_id = :source_id AND name = :name AND source = :source";
156
		$query_values = array(':name' => $name,':source' => $source,':source_id' => $source_id,':description' => $description);
157
		try {
158
			$sth = $this->db->prepare($query);
159
			$sth->execute($query_values);
160
		} catch(PDOException $e) {
161
			return "error : ".$e->getMessage();
162
		}
163
		return '';
164
	}
165
166
	public function updateLocationByLocationID($name,$latitude,$longitude,$altitude,$city,$country,$source,$logo = 'antenna.png',$type = '',$source_id = 0, $location_id,$last_seen = '',$description = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
		if ($last_seen == '') $last_seen = date('Y-m-d H:i:s');
168
		$query = "UPDATE source_location SET latitude = :latitude,longitude = :longitude,altitude = :altitude,country = :country,city = :city,logo = :logo,type = :type, last_seen = :last_seen, description = :description WHERE location_id = :location_id AND source = :source AND source_id = :source_id";
169
		$query_values = array(':source_id' => $source_id,':latitude' => $latitude, ':longitude' => $longitude,':altitude' => $altitude,':city' => $city,':country' => $country,':logo' => $logo,':source' => $source,':type' => $type,':last_seen' => $last_seen,':location_id' => $location_id,':description' => $description);
170
		try {
171
			$sth = $this->db->prepare($query);
172
			$sth->execute($query_values);
173
		} catch(PDOException $e) {
174
			echo "error : ".$e->getMessage();
175
		}
176
	}
177
178
	public function deleteLocation($id) {
179
		$query = "DELETE FROM source_location WHERE id = :id";
180
		$query_values = array(':id' => $id);
181
		try {
182
			$sth = $this->db->prepare($query);
183
			$sth->execute($query_values);
184
		} catch(PDOException $e) {
185
			return "error : ".$e->getMessage();
186
		}
187
		return '';
188
	}
189
190
	public function deleteLocationByType($type) {
191
		$query = "DELETE FROM source_location WHERE type = :type";
192
		$query_values = array(':type' => $type);
193
		try {
194
			$sth = $this->db->prepare($query);
195
			$sth->execute($query_values);
196
		} catch(PDOException $e) {
197
			return "error : ".$e->getMessage();
198
		}
199
		return '';
200
	}
201
202
	public function deleteLocationBySource($source) {
203
		$query = "DELETE FROM source_location WHERE source = :source";
204
		$query_values = array(':source' => $source);
205
		try {
206
			$sth = $this->db->prepare($query);
207
			$sth->execute($query_values);
208
		} catch(PDOException $e) {
209
			return "error : ".$e->getMessage();
210
		}
211
		return '';
212
	}
213
214
	public function deleteAllLocation() {
215
		$query = "DELETE FROM source_location";
216
		try {
217
			$sth = $this->db->prepare($query);
218
			$sth->execute();
219
		} catch(PDOException $e) {
220
			return "error : ".$e->getMessage();
221
		}
222
		return '';
223
	}
224
225
	public function deleteOldLocationByType($type) {
226
		global $globalDBdriver;
227
		if ($type == 'wx') {
228
			if ($globalDBdriver == 'mysql') {
229
				$query  = "DELETE FROM source_location WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 DAY) >= source_location.last_seen AND type = :type";
230
			} else {
231
				$query  = "DELETE FROM source_location WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '1 DAY' >= source_location.last_seen AND type = :type";
232
			}
233
		} elseif ($type == 'lightning') {
234
			if ($globalDBdriver == 'mysql') {
235
				$query  = "DELETE FROM source_location WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 20 MINUTE) >= source_location.last_seen AND type = :type";
236
			} else {
237
				$query  = "DELETE FROM source_location WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '20 MINUTE' >= source_location.last_seen AND type = :type";
238
			}
239
		} else {
240
			if ($globalDBdriver == 'mysql') {
241
				$query  = "DELETE FROM source_location WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 WEEK) >= source_location.last_seen AND type = :type";
242
			} else {
243
				$query  = "DELETE FROM source_location WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '1 WEEK' >= source_location.last_seen AND type = :type";
244
			}
245
		}
246
		try {
247
			$sth = $this->db->prepare($query);
248
			$sth->execute(array(':type' => $type));
249
		} catch(PDOException $e) {
250
			return "error";
251
		}
252
		return "success";
253
	}
254
}
255
?>