5eed7262d1bbd16b246ec69f8f121b493f840a56
[gamestick-pjgsapi.git] / src / ProfileDb.php
1 <?php
2 require_once 'Profile.php';
3
4 /**
5  * Functions to work with player profiles
6  */
7 class ProfileDb
8 {
9     protected PDO $db;
10
11     public function __construct()
12     {
13         $dbFile = dirname(__FILE__, 2) . '/data/profiles.sqlite3';
14         $this->db = new PDO(
15             'sqlite:' . $dbFile,
16             '', '',
17             [
18                 PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
19                 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
20             ]
21         );
22         $this->createTablesIfNeeded();
23     }
24
25     public function getProfileByHardwareId(string $hwId): ?Profile
26     {
27         $stmt = $this->db->prepare('SELECT * FROM gamesticks WHERE hwId = :id');
28         $stmt->setFetchMode(PDO::FETCH_CLASS, 'Profile');
29         $stmt->execute([':id' => $hwId]);
30         $row = $stmt->fetch();
31         return $row === false ? null : $row;
32     }
33
34     public function getProfileBySessionId(string $sessionId): ?Profile
35     {
36         $stmt = $this->db->prepare('SELECT * FROM gamesticks WHERE sessionId = :id');
37         $stmt->setFetchMode(PDO::FETCH_CLASS, 'Profile');
38         $stmt->execute([':id' => $sessionId]);
39         $row = $stmt->fetch();
40         return $row === false ? null : $row;
41     }
42
43     public function getProfileByVerificationCode(string $code): ?Profile
44     {
45         $stmt = $this->db->prepare('SELECT * FROM gamesticks WHERE verificationCode = :code');
46         $stmt->setFetchMode(PDO::FETCH_CLASS, 'Profile');
47         $stmt->execute([':code' => $code]);
48         $row = $stmt->fetch();
49         return $row === false ? null : $row;
50     }
51
52     public function createProfile(string $hwId): Profile
53     {
54         $stmt = $this->db->prepare(
55             <<<SQL
56             INSERT INTO gamesticks
57             (hwId, sessionId, verificationCode)
58             VALUES (:hwId, :sessionId, :verificationCode)
59 SQL
60         );
61         $stmt->execute(
62             [
63                 ':hwId'             => $hwId,
64                 ':sessionId'        => 's' . str_replace(':', '', $hwId),
65                 ':verificationCode' => date('His'),
66             ]
67         );
68         return $this->getProfileByHardwareId($hwId);
69     }
70
71     public function updateProfile(string $hwId, array $values): ?Profile
72     {
73         $params = [
74             'hwId' => $hwId,
75         ];
76
77         $sql = 'UPDATE gamesticks SET';
78         $sqlParts = [];
79         foreach ($values as $column => $value) {
80             $sqlParts[] = ' ' . $column . '= :' . $column;
81             $params[':' . $column] = $value;
82         }
83         $sql .= implode(', ', $sqlParts) . ' WHERE hwId = :hwId';
84
85         $stmt = $this->db->prepare($sql);
86         $stmt->execute($params);
87
88         return $this->getProfileByHardwareId($hwId);
89     }
90
91     protected function createTablesIfNeeded()
92     {
93         $res = $this->db->query(
94             'SELECT name FROM sqlite_master WHERE type = "table" AND name = "gamesticks"'
95         )->fetch();
96         if ($res !== false) {
97             return;
98         }
99
100         $this->db->exec(
101             <<<SQL
102         CREATE TABLE gamesticks (
103             id INTEGER PRIMARY KEY AUTOINCREMENT,
104             hwId TEXT NOT NULL,
105             sessionId TEXT,
106             verificationCode TEXT DEFAULT NULL,
107             gamerTag TEXT DEFAULT NULL,
108             founderFlag INTEGER DEFAULT 0 NOT NULL,
109             founderName TEXT DEFAULT NULL,
110             minAge INTEGER DEFAULT 3 NOT NULL,
111             avatar TEXT DEFAULT NULL,
112
113             created_at TEXT DEFAULT CURRENT_TIMESTAMP
114         )
115 SQL
116         );
117     }
118 }
119 ?>