Satellite   B
last analyzed

Complexity

Total Complexity 47

Size/Duplication

Total Lines 298
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 298
rs 8.64
c 0
b 0
f 0
wmc 47
lcom 1
cbo 6

14 Methods

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