com web/bugs: Add Vote repository class: src/Repository/VoteRepository.php www/vote.php
| From: | Peter Kokot | Date: | Wed, 19 Dec 2018 02:11:46 +0000 |
| Subject: | com web/bugs: Add Vote repository class: src/Repository/VoteRepository.php www/vote.php | ||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-27597@lists.php.net to get a copy of this message | ||
Commit: 9a8dbe29c491d31872421f91807667473fedf248
Author: Peter Kokot <peterkokot@gmail.com> Wed, 19 Dec 2018 03:11:46 +0100
Parents: a1e9fe6f6a7750b008dfe92cf8ecd9ab877b3f67
Branches: master
Link: http://git.php.net/?p=web/bugs.git;a=commitdiff;h=9a8dbe29c491d31872421f91807667473fedf248
Log:
Add Vote repository class
Changed paths:
A src/Repository/VoteRepository.php
M www/vote.php
Diff:
diff --git a/src/Repository/VoteRepository.php b/src/Repository/VoteRepository.php
new file mode 100644
index 0000000..e832e7c
--- /dev/null
+++ b/src/Repository/VoteRepository.php
@@ -0,0 +1,42 @@
+<?php
+
+namespace App\Repository;
+
+/**
+ * Repository class for fetching data from the bugdb_votes database table.
+ */
+class VoteRepository
+{
+ /**
+ * Database handle.
+ * @var \PDO
+ */
+ private $dbh;
+
+ /**
+ * Class constructor
+ */
+ public function __construct(\PDO $dbh)
+ {
+ $this->dbh = $dbh;
+ }
+
+ /**
+ * Find vote row by bug id and IP.
+ */
+ public function findOneByIdAndIp(int $id, string $ip): array
+ {
+ $sql = 'SELECT bug, ip FROM bugdb_votes WHERE bug = ? AND ip = ? LIMIT 1';
+
+ $statement = $this->dbh->prepare($sql);
+ $statement->execute([$id, $ip]);
+
+ $result = $statement->fetch();
+
+ if ($result === false) {
+ return [];
+ }
+
+ return $result;
+ }
+}
diff --git a/www/vote.php b/www/vote.php
index 73f2a9c..e5d6817 100644
--- a/www/vote.php
+++ b/www/vote.php
@@ -1,6 +1,7 @@
<?php
use App\Repository\BugRepository;
+use App\Repository\VoteRepository;
// Obtain common includes
require_once '../include/prepend.php';
@@ -63,11 +64,7 @@ $ip = ip2long(get_real_ip());
// TODO: check if ip address has been banned. hopefully this will never need to be implemented.
// Check whether the user has already voted on this bug.
-$bug_check = $dbh->prepare("SELECT bug, ip FROM bugdb_votes WHERE bug = ? AND ip = ? LIMIT
1")
- ->execute([$id, $ip])
- ->fetch(\PDO::FETCH_BOTH);
-
-if (empty($bug_check)) {
+if (empty((new VoteRepository($dbh))->findOneByIdAndIp($id, $ip))) {
// If the user vote isn't found, create one.
$dbh->prepare("
INSERT INTO bugdb_votes (bug, ip, score, reproduced, tried, sameos, samever)