Completed
Push — master ( 8eb35e...77cd33 )
by Yannick
09:48
created

ATC::add()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 6
nop 12
dl 0
loc 15
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 ATC {
6
	public $db;
7
	public function __construct($dbc = null) {
8
		$Connection = new Connection($dbc);
9
		$this->db = $Connection->db;
10
	}
11
	
12
	
13
    /**
14
    * Get SQL query part for filter used
15
    * @param Array $filter the filter
16
    * @return Array the SQL part
17
    */
18
    public function getFilter($filter = array(),$where = false,$and = false) {
19
	global $globalFilter, $globalStatsFilters, $globalFilterName;
20
	if (is_array($globalStatsFilters) && isset($globalStatsFilters[$globalFilterName])) {
21
	    if (isset($globalStatsFilters[$globalFilterName][0]['source'])) {
22
		foreach($globalStatsFilters[$globalFilterName] as $source) {
23
			if (isset($source['source'])) $filter['source'][] = $source['source'];
24
		}
25
	    } else {
26
		$filter = $globalStatsFilters[$globalFilterName];
27
	    }
28
	}
29
	if (is_array($globalFilter)) $filter = array_merge($filter,$globalFilter);
30
	$filter_query_join = '';
31
	$filter_query_where = '';
32
	if (isset($filter['source']) && !empty($filter['source'])) {
33
	    $filter_query_where = " WHERE format_source IN ('".implode("','",$filter['source'])."')";
34
	}
35
	if ($filter_query_where == '' && $where) $filter_query_where = ' WHERE';
36
	elseif ($filter_query_where != '' && $and) $filter_query_where .= ' AND';
37
	$filter_query = $filter_query_join.$filter_query_where;
38
	return $filter_query;
39
    }
40
41
       public function getAll() {
42
    		$filter_query = $this->getFilter(array());
43
                $query = "SELECT * FROM atc".$filter_query;
44
                $query_values = array();
45
                 try {
46
                        $sth = $this->db->prepare($query);
47
                        $sth->execute($query_values);
48
                } catch(PDOException $e) {
49
                        return "error : ".$e->getMessage();
50
                }
51
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
52
                return $all;
53
        }
54
55
	public function getById($id) {
56
    		$filter_query = $this->getFilter(array(),true);
57
                $query = "SELECT * FROM atc".$filter_query." atc_id = :id";
58
                $query_values = array(':id' => $id);
59
                 try {
60
                        $sth = $this->db->prepare($query);
61
                        $sth->execute($query_values);
62
                } catch(PDOException $e) {
63
                        return "error : ".$e->getMessage();
64
                }
65
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
66
                return $all;
67
        }
68
69
	public function getByIdent($ident,$format_source = '') {
70
		$filter_query = $this->getFilter(array(),true);
71
		if ($format_source == '') {
72
			$query = "SELECT * FROM atc".$filter_query." ident = :ident";
73
			$query_values = array(':ident' => $ident);
74
		} else {
75
			$query = "SELECT * FROM atc".$filter_query." ident = :ident AND format_source = :format_source";
76
			$query_values = array(':ident' => $ident,':format_source' => $format_source);
77
		}
78
		try {
79
			$sth = $this->db->prepare($query);
80
			$sth->execute($query_values);
81
		} catch(PDOException $e) {
82
			return "error : ".$e->getMessage();
83
		}
84
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
85
		return $all;
86
	}
87
88
	public function add($ident,$frequency,$latitude,$longitude,$range,$info,$date,$type = '',$ivao_id = '',$ivao_name = '',$format_source = '',$source_name = '') {
89
		$info = preg_replace('/[^(\x20-\x7F)]*/','',$info);
90
		$info = str_replace('^','<br />',$info);
91
		$info = str_replace('&amp;sect;','',$info);
92
		$info = str_replace('"','',$info);
93
		if ($type == '') $type = NULL;
94
		$query = "INSERT INTO atc (ident,frequency,latitude,longitude,atc_range,info,atc_lastseen,type,ivao_id,ivao_name,format_source,source_name) VALUES (:ident,:frequency,:latitude,:longitude,:range,:info,:date,:type,:ivao_id,:ivao_name,:format_source,:source_name)";
95
		$query_values = array(':ident' => $ident,':frequency' => $frequency,':latitude' => $latitude,':longitude' => $longitude,':range' => $range,':info' => $info,':date' => $date,':ivao_id' => $ivao_id,':ivao_name' => $ivao_name, ':type' => $type,':format_source' => $format_source,':source_name' => $source_name);
96
		try {
97
			$sth = $this->db->prepare($query);
98
			$sth->execute($query_values);
99
		} catch(PDOException $e) {
100
			return "error : ".$e->getMessage();
101
		}
102
	}
103
104
	public function update($ident,$frequency,$latitude,$longitude,$range,$info,$date,$type = '',$ivao_id = '',$ivao_name = '',$format_source = '',$source_name = '') {
105
		$info = preg_replace('/[^(\x20-\x7F)]*/','',$info);
106
		$info = str_replace('^','<br />',$info);
107
		$info = str_replace('&amp;sect;','',$info);
108
		$info = str_replace('"','',$info);
109
		if ($type == '') $type = NULL;
110
		$query = "UPDATE atc SET frequency = :frequency,latitude = :latitude,longitude = :longitude,atc_range = :range,info = :info,atc_lastseen = :date,type = :type,ivao_id = :ivao_id,ivao_name = :ivao_name WHERE ident = :ident AND format_source = :format_source AND source_name = :source_name";
111
		$query_values = array(':ident' => $ident,':frequency' => $frequency,':latitude' => $latitude,':longitude' => $longitude,':range' => $range,':info' => $info,':date' => $date,':ivao_id' => $ivao_id,':ivao_name' => $ivao_name, ':type' => $type,':format_source' => $format_source,':source_name' => $source_name);
112
		try {
113
			$sth = $this->db->prepare($query);
114
			$sth->execute($query_values);
115
		} catch(PDOException $e) {
116
			return "error : ".$e->getMessage();
117
		}
118
	}
119
120
       public function deleteById($id) {
121
                $query = "DELETE FROM atc WHERE atc_id = :id";
122
                $query_values = array(':id' => $id);
123
                 try {
124
                        $sth = $this->db->prepare($query);
125
                        $sth->execute($query_values);
126
                } catch(PDOException $e) {
127
                        return "error : ".$e->getMessage();
128
                }
129
        }
130
131
       public function deleteByIdent($ident,$format_source) {
132
                $query = "DELETE FROM atc WHERE ident = :ident AND format_source = :format_source";
133
                $query_values = array(':ident' => $ident,':format_source' => $format_source);
134
                 try {
135
                        $sth = $this->db->prepare($query);
136
                        $sth->execute($query_values);
137
                } catch(PDOException $e) {
138
                        return "error : ".$e->getMessage();
139
                }
140
        }
141
142
       public function deleteAll() {
143
                $query = "DELETE FROM atc";
144
                $query_values = array();
145
                 try {
146
                        $sth = $this->db->prepare($query);
147
                        $sth->execute($query_values);
148
                } catch(PDOException $e) {
149
                        return "error : ".$e->getMessage();
150
                }
151
        }
152
153
	public function deleteOldATC() {
154
                global $globalDBdriver;
155
                if ($globalDBdriver == 'mysql') {
156
                        $query  = "DELETE FROM atc WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) >= atc.atc_lastseen";
157
                } else {
158
                        $query  = "DELETE FROM atc WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '1 HOUR' >= atc.atc_lastseen";
159
                }
160
                try {
161
                        $sth = $this->db->prepare($query);
162
                        $sth->execute();
163
                } catch(PDOException $e) {
164
                        return "error";
165
                }
166
                return "success";
167
        }
168
}
169
?>