Skip to content

Commit

Permalink
Update Persistence.php
Browse files Browse the repository at this point in the history
  • Loading branch information
rdorado committed Nov 12, 2018
1 parent 3728cc7 commit 7b565c8
Showing 1 changed file with 1 addition and 79 deletions.
80 changes: 1 addition & 79 deletions src/Persistence.php
Expand Up @@ -187,63 +187,9 @@ public static function checkOptionsTable($pdo, $config) {
return true;
}

/*
* @param $pdo PDO
* @param $config Configuration
*/
public static function checkHitTable($pdo, $config) {
try {
$db_hit_table = $config->getHitTableName();
$stmt = $pdo->prepare("SELECT * FROM $db_hit_table WHERE 1==0");
$stmt->execute();
} catch (Exception $e) {
return false;
}
return true;
}

/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function countHit($pdo, $config, $options = []) {
if (array_key_exists('url', $options)) {
$url = $options['url'];
} else if (array_key_exists('HTTP_HOST', $_SERVER)) {
$url = "http://".$_SERVER['HTTP_HOST'];
if (array_key_exists('REQUEST_URI', $_SERVER)) {
$url = $url.$_SERVER['REQUEST_URI'];
}
} else {
$url = "No Info";
}

if ($config->getRemoveQueryString()) {
$url = preg_replace('/\?.*/', '', $url);
}

if (array_key_exists('id', $options)) {
$id = $options['id'];
} else {
$id = $url;
}

$stmt = $pdo->prepare("UPDATE $db_hit_table SET count = count + 1 WHERE id = ?");
$stmt->execute([$id]);
if ($stmt->rowCount()==0) {
$stmt = $pdo->prepare("INSERT INTO $db_hit_table (id, count) VALUES (?, 1)");
$stmt->execute([$id]);
}


$stmt = $pdo->prepare("UPDATE $db_url_table SET count = count + 1 WHERE id = ? and url = ?");
$stmt->execute([$id, $url]);
if ($stmt->rowCount()==0) {
$stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)");
$stmt->execute([$id, $url]);
}

}

/*
* @param $pdo PDO
Expand Down Expand Up @@ -310,8 +256,6 @@ public static function updateCount($pdo, $config, $options = []) {
Persistence::countHit($pdo, $config, $options);
Persistence::countFrom($pdo, $config, $options);
Persistence::countOptions($pdo, $config, $options);



return true;
}
Expand All @@ -338,28 +282,6 @@ public static function findHitIdsByUrl($pdo, $config, $url) {
return $resp;
}


/*
* @param $pdo PDO
* @param $config Configuration
*
*/
public static function getAllHits($pdo, $config) {
$resp = [];
try {
$dbtable = $config->getHitTableName();
$stmt = $pdo->prepare("SELECT id,count FROM $dbtable");
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
$resp[] = [$row['id'], $row['count']];
}
}

} catch (Exception $e) {
throw new Exception("Error executing function 'getAllHits'. ".$e->getMessage());
}
return $resp;
}


/*
Expand Down

0 comments on commit 7b565c8

Please sign in to comment.