Completed
Push — master ( d35909...f740b5 )
by Yannick
06:46
created

Satellite::get_tle()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 6
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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 = 37.790252;
89
		$qth->lon = -122.419968;
0 ignored issues
show
Documentation Bug introduced by
The property $lon was declared of type double, but -122.419968 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
90
	
91
		$tle_file = $this->get_tle($name);
92
		//print_r($tle_file);
93
		$type = $tle_file['tle_type'];
94
		$tle = new Predict_TLE($tle_file['tle_name'],$tle_file['tle_tle1'],$tle_file['tle_tle2']);
95
		$sat = new Predict_Sat($tle);
96
		$predict = new Predict();
97
		//if ($timestamp == '') $now = Predict_Time::get_current_daynum();
98
		if ($timestamp_begin == '') $timestamp_begin = time();
99
		if ($timestamp_end == '') {
100
			$now = Predict_Time::unix2daynum($timestamp_begin);
101
			//echo $now;
102
			$predict->predict_calc($sat,$qth,$now);
103
			return array('name' => $name, 'latitude' => $sat->ssplat,'longitude' => $sat->ssplon, 'altitude' => $sat->alt,'speed' => $sat->velo*60*60,'timestamp' => $timestamp_begin,'type' => $type);
104
		} else {
105
			for ($timestamp = $timestamp_begin; $timestamp <= $timestamp_end; $timestamp=$timestamp+$second) {
106
				//echo $timestamp."\n";
107
				$now = Predict_Time::unix2daynum($timestamp);
108
				//echo $now;
109
				$predict->predict_calc($sat,$qth,$now);
110
				$result[] = array('name' => $name,'latitude' => $sat->ssplat,'longitude' => $sat->ssplon, 'altitude' => $sat->alt,'speed' => $sat->velo*60*60,'timestamp' => $timestamp,'type' => $type);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
111
			}
112
			return $result;
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
113
		}
114
	}
115
}
116
/*
117
$sat = new Satellite();
118
print_r($sat->position('ISS (ZARYA)',time(),time()+100));
119
print_r($sat->get_tle_types());
120
*/
121
?>