@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | namespace geolocation\bin; |
| 4 | 4 | |
| 5 | 5 | use \PDO; |
| 6 | -use \SplFileInfo ; |
|
| 6 | +use \SplFileInfo; |
|
| 7 | 7 | use \Throwable; |
| 8 | 8 | |
| 9 | 9 | class GeoipDatabase |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | */ |
| 87 | 87 | private function genDsn(string $database = null) |
| 88 | 88 | { |
| 89 | - $database || $database='Geoip.db.sqlite'; |
|
| 89 | + $database || $database = 'Geoip.db.sqlite'; |
|
| 90 | 90 | try { |
| 91 | 91 | $destination = rtrim(dirname(__DIR__), self::DS); |
| 92 | 92 | if (!is_writeable($destination)) |
@@ -94,15 +94,15 @@ discard block |
||
| 94 | 94 | throw new Throwable(sprintf('The required destination path is not writable: `%s`', $destination)); |
| 95 | 95 | } |
| 96 | 96 | $info = new SplFileInfo($database); |
| 97 | - $dbName= $info->getFilename(); |
|
| 98 | - $dbSuffix='.sqlite'; |
|
| 99 | - if (substr_compare(strtolower($dbName), $dbSuffix, -strlen($dbSuffix)) !== 0) { $dbName .= $dbSuffix ; } |
|
| 97 | + $dbName = $info->getFilename(); |
|
| 98 | + $dbSuffix = '.sqlite'; |
|
| 99 | + if (substr_compare(strtolower($dbName), $dbSuffix, -strlen($dbSuffix)) !== 0) { $dbName .= $dbSuffix; } |
|
| 100 | 100 | } catch (Throwable $th) { |
| 101 | 101 | trigger_error($th->getMessage(), E_USER_ERROR); |
| 102 | 102 | } |
| 103 | 103 | $destination .= self::DS.'data'; |
| 104 | 104 | if (!is_dir($destination)) { mkdir($destination, '0755', true); } |
| 105 | - return 'sqlite:'.realpath($destination).self::DS.$dbName ; |
|
| 105 | + return 'sqlite:'.realpath($destination).self::DS.$dbName; |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | /** |
@@ -163,16 +163,16 @@ discard block |
||
| 163 | 163 | $sCommand .= 'FROM `ipv%dRange` '; |
| 164 | 164 | $sCommand .= 'WHERE `start` <= :start '; |
| 165 | 165 | $sCommand .= 'ORDER BY start DESC LIMIT 1'; |
| 166 | - $statement = $this->oPDOInstance->prepare(sprintf($sCommand, $ipVersion)) ; |
|
| 167 | - $statement->execute([':start' => $start ]) ; |
|
| 168 | - $row = $statement->fetch(PDO::FETCH_OBJ) ; |
|
| 166 | + $statement = $this->oPDOInstance->prepare(sprintf($sCommand, $ipVersion)); |
|
| 167 | + $statement->execute([':start' => $start]); |
|
| 168 | + $row = $statement->fetch(PDO::FETCH_OBJ); |
|
| 169 | 169 | if (is_bool($row) && $row === false) |
| 170 | 170 | { |
| 171 | 171 | $row = new \stdClass(); |
| 172 | - $row->end = 0 ; |
|
| 172 | + $row->end = 0; |
|
| 173 | 173 | } |
| 174 | - if ($row->end < $start || !$row->country) { $row->country = 'ZZ' ; } |
|
| 175 | - return $row->country ; |
|
| 174 | + if ($row->end < $start || !$row->country) { $row->country = 'ZZ'; } |
|
| 175 | + return $row->country; |
|
| 176 | 176 | } catch (\PDOException $th) { |
| 177 | 177 | trigger_error($th->getMessage(), E_USER_ERROR); |
| 178 | 178 | } |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | * @param array $tablesList |
| 185 | 185 | * @return void |
| 186 | 186 | */ |
| 187 | - public function flush(array $tablesList=[]) |
|
| 187 | + public function flush(array $tablesList = []) |
|
| 188 | 188 | { |
| 189 | 189 | !empty($tablesList) || $tablesList = $this->showTables(); |
| 190 | 190 | is_array($tablesList) || $tablesList = [$tablesList]; |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | $this->oPDOInstance->query('VACUUM'); |
| 199 | 199 | endif; |
| 200 | 200 | } catch (\PDOException $th) { |
| 201 | - trigger_error('Statement failed: ' . $th->getMessage(), E_USER_ERROR); |
|
| 201 | + trigger_error('Statement failed: '.$th->getMessage(), E_USER_ERROR); |
|
| 202 | 202 | } |
| 203 | 203 | } |
| 204 | 204 | |
@@ -215,17 +215,17 @@ discard block |
||
| 215 | 215 | { |
| 216 | 216 | try |
| 217 | 217 | { |
| 218 | - $sQuery ='INSERT INTO `ipv%dRange` (`start`, `end`, `country`) values (:start, :end, :country)'; |
|
| 218 | + $sQuery = 'INSERT INTO `ipv%dRange` (`start`, `end`, `country`) values (:start, :end, :country)'; |
|
| 219 | 219 | $command = sprintf($sQuery, $ipVersion); |
| 220 | 220 | $statement = $this->oPDOInstance->prepare($command); |
| 221 | 221 | $statement->execute([ |
| 222 | 222 | ':start' => $start, |
| 223 | 223 | ':end' => $end, |
| 224 | 224 | ':country' => $country |
| 225 | - ]) ; |
|
| 225 | + ]); |
|
| 226 | 226 | return $this->oPDOInstance->lastInsertId(); |
| 227 | 227 | } catch (\PDOException $th) { |
| 228 | - trigger_error('Statement failed: ' . $th->getMessage(), E_USER_ERROR); |
|
| 228 | + trigger_error('Statement failed: '.$th->getMessage(), E_USER_ERROR); |
|
| 229 | 229 | } |
| 230 | 230 | } |
| 231 | 231 | /** |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | */ |
| 236 | 236 | public function beginTransaction() |
| 237 | 237 | { |
| 238 | - if (!$this->transactionCounter++) {return $this->oPDOInstance->beginTransaction();} |
|
| 238 | + if (!$this->transactionCounter++) {return $this->oPDOInstance->beginTransaction(); } |
|
| 239 | 239 | return $this->transactionCounter >= 0; |
| 240 | 240 | } |
| 241 | 241 | |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | */ |
| 247 | 247 | public function commit() |
| 248 | 248 | { |
| 249 | - if (!--$this->transactionCounter) {return $this->oPDOInstance->commit();} |
|
| 249 | + if (!--$this->transactionCounter) {return $this->oPDOInstance->commit(); } |
|
| 250 | 250 | return $this->transactionCounter >= 0; |
| 251 | 251 | } |
| 252 | 252 | |