Completed
Push — master ( bc6eda...6cdd6f )
by Yannick
09:22
created

Source::addLocation()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 6
nop 13
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
require_once(dirname(__FILE__).'/settings.php');
3
require_once(dirname(__FILE__).'/class.Connection.php');
4
5
class Source {
6
	public $db;
7
	public function __construct($dbc = null) {
8
		$Connection = new Connection($dbc);
9
		$this->db = $Connection->db;
10
	}
11
12
	public function getAllLocationInfo() {
13
		$query = "SELECT * FROM source_location";
14
		$query_values = array();
15
		try {
16
			$sth = $this->db->prepare($query);
17
			$sth->execute($query_values);
18
		} catch(PDOException $e) {
19
			return "error : ".$e->getMessage();
20
		}
21
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
22
		return $all;
23
	}
24
25
	public function getLocationInfobyName($name) {
26
		$query = "SELECT * FROM source_location WHERE name = :name";
27
		$query_values = array(':name' => $name);
28
		try {
29
			$sth = $this->db->prepare($query);
30
			$sth->execute($query_values);
31
		} catch(PDOException $e) {
32
			return "error : ".$e->getMessage();
33
		}
34
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
35
		return $all;
36
	}
37
38
	public function getLocationInfobySourceName($name) {
39
		$query = "SELECT * FROM source_location WHERE source = :name";
40
		$query_values = array(':name' => $name);
41
		try {
42
			$sth = $this->db->prepare($query);
43
			$sth->execute($query_values);
44
		} catch(PDOException $e) {
45
			return "error : ".$e->getMessage();
46
		}
47
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
48
		return $all;
49
	}
50
51
	public function getLocationInfoByType($type) {
52
		$query = "SELECT * FROM source_location WHERE type = :type";
53
		$query_values = array(':type' => $type);
54
		try {
55
			$sth = $this->db->prepare($query);
56
			$sth->execute($query_values);
57
		} catch(PDOException $e) {
58
			return "error : ".$e->getMessage();
59
		}
60
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
61
		return $all;
62
	}
63
64
	public function getLocationInfoByLocationID($location_id) {
65
		$query = "SELECT * FROM source_location WHERE location_id = :location_id";
66
		$query_values = array(':location_id' => $location_id);
67
		try {
68
			$sth = $this->db->prepare($query);
69
			$sth->execute($query_values);
70
		} catch(PDOException $e) {
71
			return "error : ".$e->getMessage();
72
		}
73
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
74
		return $all;
75
	}
76
77
	public function getLocationInfoByID($id) {
78
		$query = "SELECT * FROM source_location WHERE id = :id";
79
		$query_values = array(':id' => $id);
80
		try {
81
			$sth = $this->db->prepare($query);
82
			$sth->execute($query_values);
83
		} catch(PDOException $e) {
84
			return "error : ".$e->getMessage();
85
		}
86
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
87
		return $all;
88
	}
89
90
	public function addLocation($name,$latitude,$longitude,$altitude,$city,$country,$source,$logo = 'antenna.png',$type = '',$source_id = 0,$location_id = 0,$last_seen = '', $description = '') {
91
		if ($last_seen == '') $last_seen = date('Y-m-d H:i:s');
92
		$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)";
93
		$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);
94
		try {
95
			$sth = $this->db->prepare($query);
96
			$sth->execute($query_values);
97
		} catch(PDOException $e) {
98
			echo "error : ".$e->getMessage();
99
		}
100
	}
101
102
	public function updateLocation($name,$latitude,$longitude,$altitude,$city,$country,$source,$logo = 'antenna.png',$type = '',$source_id = 0,$location_id = 0,$last_seen = '',$description = '') {
103
		if ($last_seen == '') $last_seen = date('Y-m-d H:i:s');
104
		$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";
105
		$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);
106
		try {
107
			$sth = $this->db->prepare($query);
108
			$sth->execute($query_values);
109
		} catch(PDOException $e) {
110
			return "error : ".$e->getMessage();
111
		}
112
	}
113
114
	public function updateLocationDescByName($name,$source,$source_id = 0,$description = '') {
115
		if ($last_seen == '') $last_seen = date('Y-m-d H:i:s');
0 ignored issues
show
Bug introduced by
The variable $last_seen seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Unused Code introduced by
$last_seen is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
116
		$query = "UPDATE source_location SET description = :description WHERE source_id = :source_id AND name = :name AND source = :source";
117
		$query_values = array(':name' => $name,':source' => $source,':source_id' => $source_id,':description' => $description);
118
		try {
119
			$sth = $this->db->prepare($query);
120
			$sth->execute($query_values);
121
		} catch(PDOException $e) {
122
			return "error : ".$e->getMessage();
123
		}
124
	}
125
126
	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...
127
		if ($last_seen == '') $last_seen = date('Y-m-d H:i:s');
128
		$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";
129
		$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);
130
		try {
131
			$sth = $this->db->prepare($query);
132
			$sth->execute($query_values);
133
		} catch(PDOException $e) {
134
			echo "error : ".$e->getMessage();
135
		}
136
	}
137
138
	public function deleteLocation($id) {
139
		$query = "DELETE FROM source_location WHERE id = :id";
140
		$query_values = array(':id' => $id);
141
		try {
142
			$sth = $this->db->prepare($query);
143
			$sth->execute($query_values);
144
		} catch(PDOException $e) {
145
			return "error : ".$e->getMessage();
146
		}
147
	}
148
149
	public function deleteLocationByType($type) {
150
		$query = "DELETE FROM source_location WHERE type = :type";
151
		$query_values = array(':type' => $type);
152
		try {
153
			$sth = $this->db->prepare($query);
154
			$sth->execute($query_values);
155
		} catch(PDOException $e) {
156
			return "error : ".$e->getMessage();
157
		}
158
	}
159
160
	public function deleteAllLocation() {
161
		$query = "DELETE FROM source_location";
162
		try {
163
			$sth = $this->db->prepare($query);
164
			$sth->execute();
165
		} catch(PDOException $e) {
166
			return "error : ".$e->getMessage();
167
		}
168
	}
169
170
	public function deleteOldLocationByType($type) {
171
		global $globalDBdriver;
172
		if ($globalDBdriver == 'mysql') {
173
			$query  = "DELETE FROM source_location WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 WEEK) >= source_location.last_seen AND type = :type";
174
		} else {
175
			$query  = "DELETE FROM source_location WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '1 WEEK' >= source_location.last_seen AND type = :type";
176
		}
177
		try {
178
			$sth = $this->db->prepare($query);
179
			$sth->execute(array(':type' => $type));
180
		} catch(PDOException $e) {
181
			return "error";
182
		}
183
		return "success";
184
	}
185
}
186
?>