Completed
Push — master ( 4b9f50...005bcb )
by Yannick
28:37
created

Satellite   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 293
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 293
rs 8.3673
c 0
b 0
f 0
wmc 45
lcom 1
cbo 6

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get_tle() 0 12 3
A get_tle_types() 0 12 3
A get_tle_names() 0 12 3
A get_tle_names_type() 0 12 3
A position_all() 0 9 2
A position_all_type() 0 9 2
B position() 0 26 4
A get_info() 0 12 3
A countAllLaunchSite() 0 22 3
A countAllOwners() 0 22 3
A countAllCountriesOwners() 0 22 3
B countAllMonthsLastYear() 0 38 6
B countAllYears() 0 37 6

How to fix   Complexity   

Complex Class

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

1
<?php
2
require_once(dirname(__FILE__).'/class.Connection.php');
3
require_once(dirname(__FILE__).'/libs/Predict/Predict.php');
4
require_once(dirname(__FILE__).'/libs/Predict/Predict/Sat.php');
5
require_once(dirname(__FILE__).'/libs/Predict/Predict/QTH.php');
6
require_once(dirname(__FILE__).'/libs/Predict/Predict/Time.php');
7
require_once(dirname(__FILE__).'/libs/Predict/Predict/TLE.php');
8
9
class Satellite {
10
	public $db;
11
12
	public function __construct($dbc = null) {
13
		$Connection = new Connection($dbc);
14
		$this->db = $Connection->db();
15
	}
16
17
	public function get_tle($name) {
18
		$query = 'SELECT tle_name, tle_tle1, tle_tle2, tle_type FROM tle WHERE tle_name = :name LIMIT 1';
19
		try {
20
			$sth = $this->db->prepare($query);
21
			$sth->execute(array(':name' => $name));
22
		} catch(PDOException $e) {
23
			echo $e->getMessage();
24
		}
25
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
26
		if (isset($result[0])) return $result[0];
27
		else return array();
28
	}
29
	public function get_tle_types() {
30
		$query = 'SELECT DISTINCT tle_type FROM tle ORDER BY tle_type';
31
		try {
32
			$sth = $this->db->prepare($query);
33
			$sth->execute();
34
		} catch(PDOException $e) {
35
			echo $e->getMessage();
36
		}
37
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
38
		if (isset($result[0])) return $result;
39
		else return array();
40
	}
41
	public function get_tle_names() {
42
		$query = 'SELECT DISTINCT tle_name, tle_type FROM tle';
43
		try {
44
			$sth = $this->db->prepare($query);
45
			$sth->execute();
46
		} catch(PDOException $e) {
47
			echo $e->getMessage();
48
		}
49
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
50
		if (isset($result[0])) return $result;
51
		else return array();
52
	}
53
	public function get_tle_names_type($type) {
54
		$query = 'SELECT tle_name, tle_type FROM tle WHERE tle_type = :type ORDER BY tle_name';
55
		try {
56
			$sth = $this->db->prepare($query);
57
			$sth->execute(array(':type' => $type));
58
		} catch(PDOException $e) {
59
			echo $e->getMessage();
60
		}
61
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
62
		if (isset($result[0])) return $result;
63
		else return array();
64
	}
65
	
66
	public function position_all($timestamp_begin = '',$timestamp_end = '',$second = 10) {
67
		$all_sat = $this->get_tle_names();
68
		$result = array();
69
		foreach ($all_sat as $sat) {
70
			$position = $this->position($sat['tle_name'],$timestamp_begin,$timestamp_end,$second);
71
			$result = array_merge($position,$result);
72
		}
73
		return $result;
74
	}
75
76
	public function position_all_type($type,$timestamp_begin = '',$timestamp_end = '',$second = 10) {
77
		$all_sat = $this->get_tle_names_type($type);
78
		$result = array();
79
		foreach ($all_sat as $sat) {
80
			$position = $this->position($sat['tle_name'],$timestamp_begin,$timestamp_end,$second);
81
			$result = array_merge($position,$result);
82
		}
83
		return $result;
84
	}
85
86
	public function position($name,$timestamp_begin = '',$timestamp_end = '',$second = 10) {
87
		$qth = new Predict_QTH();
88
		$qth->lat = floatval(37.790252);
89
		$qth->lon = floatval(-122.419968);
90
	
91
		$tle_file = $this->get_tle($name);
92
		$type = $tle_file['tle_type'];
93
		$tle = new Predict_TLE($tle_file['tle_name'],$tle_file['tle_tle1'],$tle_file['tle_tle2']);
94
		$sat = new Predict_Sat($tle);
95
		$predict = new Predict();
96
		//if ($timestamp == '') $now = Predict_Time::get_current_daynum();
97
		if ($timestamp_begin == '') $timestamp_begin = time();
98
		if ($timestamp_end == '') {
99
			$now = Predict_Time::unix2daynum($timestamp_begin);
100
			$predict->predict_calc($sat,$qth,$now);
101
			return array('name' => $name, 'latitude' => $sat->ssplat,'longitude' => $sat->ssplon, 'altitude' => $sat->alt,'speed' => $sat->velo*60*60,'timestamp' => $timestamp_begin,'type' => $type);
102
		} else {
103
			$result = array();
104
			for ($timestamp = $timestamp_begin; $timestamp <= $timestamp_end; $timestamp=$timestamp+$second) {
105
				$now = Predict_Time::unix2daynum($timestamp);
106
				$predict->predict_calc($sat,$qth,$now);
107
				$result[] = array('name' => $name,'latitude' => $sat->ssplat,'longitude' => $sat->ssplon, 'altitude' => $sat->alt,'speed' => $sat->velo*60*60,'timestamp' => $timestamp,'type' => $type);
108
			}
109
			return $result;
110
		}
111
	}
112
113
	public function get_info($name) {
114
		$query = 'SELECT * FROM satellite WHERE LOWER(name) LIKE :name OR LOWER(name_alternate) LIKE :name LIMIT 1';
115
		try {
116
			$sth = $this->db->prepare($query);
117
			$sth->execute(array(':name' => $name.'%'));
118
		} catch(PDOException $e) {
119
			echo $e->getMessage();
120
		}
121
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
122
		if (isset($result[0])) return $result[0];
123
		else return array();
124
	}
125
126
	/**
127
	* Gets all launch site
128
	*
129
	* @return Array the launch site list
130
	*
131
	*/
132
	public function countAllLaunchSite($limit = true, $filters = array())
0 ignored issues
show
Unused Code introduced by
The parameter $filters 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...
133
	{
134
		global $globalDBdriver;
135
		//$filter_query = $this->getFilter($filters,true,true);
136
		$filter_query = ' WHERE';
137
		$query  = "SELECT DISTINCT satellite.launch_site AS launch_site, COUNT(satellite.launch_site) AS launch_site_count
138
		    FROM satellite".$filter_query." satellite.launch_site <> '' AND satellite.launch_site IS NOT NULL";
139
		$query_values = array();
140
		$query .= " GROUP BY satellite.launch_site ORDER BY launch_site_count DESC";
141
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
142
		$sth = $this->db->prepare($query);
143
		$sth->execute($query_values);
144
		$launch_site_array = array();
145
		$temp_array = array();
146
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
147
		{
148
			$temp_array['launch_site'] = $row['launch_site'];
149
			$temp_array['launch_site_count'] = $row['launch_site_count'];
150
			$launch_site_array[] = $temp_array;
151
		}
152
		return $launch_site_array;
153
	}
154
155
	/**
156
	* Gets all owners
157
	*
158
	* @return Array the owners list
159
	*
160
	*/
161
	public function countAllOwners($limit = true, $filters = array())
0 ignored issues
show
Unused Code introduced by
The parameter $filters 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...
162
	{
163
		global $globalDBdriver;
164
		//$filter_query = $this->getFilter($filters,true,true);
165
		$filter_query = ' WHERE';
166
		$query  = "SELECT DISTINCT satellite.owner AS owner_name, COUNT(satellite.owner) AS owner_count
167
		    FROM satellite".$filter_query." satellite.owner <> '' AND satellite.owner IS NOT NULL";
168
		$query_values = array();
169
		$query .= " GROUP BY satellite.owner ORDER BY owner_count DESC";
170
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
171
		$sth = $this->db->prepare($query);
172
		$sth->execute($query_values);
173
		$owner_array = array();
174
		$temp_array = array();
175
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
176
		{
177
			$temp_array['owner_name'] = $row['owner_name'];
178
			$temp_array['owner_count'] = $row['owner_count'];
179
			$owner_array[] = $temp_array;
180
		}
181
		return $owner_array;
182
	}
183
184
	/**
185
	* Gets all countries owners
186
	*
187
	* @return Array the countries list
188
	*
189
	*/
190
	public function countAllCountriesOwners($limit = true, $filters = array())
0 ignored issues
show
Unused Code introduced by
The parameter $filters 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...
191
	{
192
		global $globalDBdriver;
193
		//$filter_query = $this->getFilter($filters,true,true);
194
		$filter_query = ' WHERE';
195
		$query  = "SELECT DISTINCT satellite.country_owner AS country_name, COUNT(satellite.country_owner) AS country_count
196
		    FROM satellite".$filter_query." satellite.country_owner <> '' AND satellite.country_owner IS NOT NULL";
197
		$query_values = array();
198
		$query .= " GROUP BY satellite.country_owner ORDER BY country_count DESC";
199
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
200
		$sth = $this->db->prepare($query);
201
		$sth->execute($query_values);
202
		$owner_array = array();
203
		$temp_array = array();
204
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
205
		{
206
			$temp_array['country_name'] = $row['country_name'];
207
			$temp_array['country_count'] = $row['country_count'];
208
			$owner_array[] = $temp_array;
209
		}
210
		return $owner_array;
211
	}
212
213
	/**
214
	* Counts all launch dates during the last year
215
	*
216
	* @return Array the launch date list
217
	*
218
	*/
219
	public function countAllMonthsLastYear($filters = array(), $sincedate = '')
0 ignored issues
show
Unused Code introduced by
The parameter $filters 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...
220
	{
221
		global $globalTimezone, $globalDBdriver;
222
		if ($globalTimezone != '') {
223
			date_default_timezone_set($globalTimezone);
224
			$datetime = new DateTime();
225
			$offset = $datetime->format('P');
226
		} else $offset = '+00:00';
227
		//$filter_query = $this->getFilter($filters,true,true);
228
		$filter_query = ' WHERE';
229
		if ($globalDBdriver == 'mysql') {
230
			$query  = "SELECT MONTH(CONVERT_TZ(satellite.launch_date,'+00:00', :offset)) AS month_name, YEAR(CONVERT_TZ(satellite.launch_date,'+00:00', :offset)) AS year_name, count(*) as date_count
231
				FROM satellite".$filter_query." satellite.launch_date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 YEAR)";
232
			if ($sincedate != '') $query .= " AND satellite.launch_date > '".$sincedate."'";
233
			$query .= " GROUP BY year_name, month_name
234
				ORDER BY year_name, month_name ASC";
235
			$query_data = array(':offset' => $offset);
236
		} else {
237
			$query  = "SELECT EXTRACT(MONTH FROM satellite.launch_date AT TIME ZONE INTERVAL :offset) AS month_name, EXTRACT(YEAR FROM satellite.launch_date AT TIME ZONE INTERVAL :offset) AS year_name, count(*) as date_count
238
				FROM satellite".$filter_query." satellite.launch_date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 YEARS'";
239
			if ($sincedate != '') $query .= " AND satellite.launch_date > '".$sincedate."'";
240
			$query .= " GROUP BY year_name, month_name
241
				ORDER BY year_name, month_name ASC";
242
			$query_data = array(':offset' => $offset);
243
		}
244
		$sth = $this->db->prepare($query);
245
		$sth->execute($query_data);
246
		$date_array = array();
247
		$temp_array = array();
248
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
249
		{
250
			$temp_array['year_name'] = $row['year_name'];
251
			$temp_array['month_name'] = $row['month_name'];
252
			$temp_array['date_count'] = $row['date_count'];
253
			$date_array[] = $temp_array;
254
		}
255
		return $date_array;
256
	}
257
258
	/**
259
	* Counts all dates during the last 10 years
260
	*
261
	* @return Array the date list
262
	*
263
	*/
264
	public function countAllYears($filters = array(), $sincedate = '')
0 ignored issues
show
Unused Code introduced by
The parameter $filters 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...
265
	{
266
		global $globalTimezone, $globalDBdriver;
267
		if ($globalTimezone != '') {
268
			date_default_timezone_set($globalTimezone);
269
			$datetime = new DateTime();
270
			$offset = $datetime->format('P');
271
		} else $offset = '+00:00';
272
		//$filter_query = $this->getFilter($filters,true,true);
273
		$filter_query = ' WHERE';
274
		if ($globalDBdriver == 'mysql') {
275
			$query  = "SELECT YEAR(CONVERT_TZ(satellite.launch_date,'+00:00', :offset)) AS year_name, count(*) as date_count
276
				FROM satellite".$filter_query." satellite.launch_date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 10 YEAR)";
277
			if ($sincedate != '') $query .= " AND satellite.launch_date > '".$sincedate."'";
278
			$query .= " GROUP BY year_name
279
				ORDER BY year_name ASC";
280
			$query_data = array(':offset' => $offset);
281
		} else {
282
			$query  = "SELECT EXTRACT(YEAR FROM satellite.launch_date AT TIME ZONE INTERVAL :offset) AS year_name, count(*) as date_count
283
				FROM satellite".$filter_query." satellite.launch_date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '10 YEARS'";
284
			if ($sincedate != '') $query .= " AND satellite.launch_date > '".$sincedate."'";
285
			$query .= " GROUP BY year_name
286
				ORDER BY year_name ASC";
287
			$query_data = array(':offset' => $offset);
288
		}
289
		$sth = $this->db->prepare($query);
290
		$sth->execute($query_data);
291
		$date_array = array();
292
		$temp_array = array();
293
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
294
		{
295
			$temp_array['year_name'] = $row['year_name'];
296
			$temp_array['date_count'] = $row['date_count'];
297
			$date_array[] = $temp_array;
298
		}
299
		return $date_array;
300
	}
301
}
302
/*
303
$sat = new Satellite();
304
print_r($sat->position('ISS (ZARYA)',time(),time()+100));
305
print_r($sat->get_tle_types());
306
*/
307
?>