mirror of
https://github.com/antonkomarev/github-profile-views-counter.git
synced 2026-07-24 14:33:37 -04:00
Add PDO repository (#2)
* Add PDO repository * Make file storage path configurable * Name username query property required * Add special exception class
This commit is contained in:
51
src/CounterPdoRepository.php
Normal file
51
src/CounterPdoRepository.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of GitHub Profile Views Counter.
|
||||
*
|
||||
* (c) Anton Komarev <anton@komarev.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Komarev\GitHubProfileViewsCounter;
|
||||
|
||||
use Contracts\Komarev\GitHubProfileViewsCounter\CounterRepositoryInterface;
|
||||
use PDO;
|
||||
|
||||
final class CounterPdoRepository implements CounterRepositoryInterface
|
||||
{
|
||||
private PDO $connection;
|
||||
|
||||
public function __construct(PDO $connection)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
public function getCountByUsername(string $username): int
|
||||
{
|
||||
$statement = $this->connection->prepare(
|
||||
'SELECT COUNT(*)
|
||||
FROM page_views
|
||||
WHERE username = :username;'
|
||||
);
|
||||
$statement->bindParam('username', $username);
|
||||
$statement->execute();
|
||||
|
||||
return (int) $statement->fetchColumn(0);
|
||||
}
|
||||
|
||||
public function incrementCountByUsername(string $username): void
|
||||
{
|
||||
$statement = $this->connection->prepare(
|
||||
'INSERT INTO page_views
|
||||
(username, created_at)
|
||||
VALUES (:username, NOW());'
|
||||
);
|
||||
$statement->bindParam('username', $username);
|
||||
$statement->execute();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user