• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

acdh-oeaw / arche-diss-cache / #57

24 Oct 2024 10:11PM UTC coverage: 80.841%. Remained the same
#57

push

php-coveralls

zozlak
Service::setCallback() introduced

6 of 6 new or added lines in 1 file covered. (100.0%)

173 of 214 relevant lines covered (80.84%)

3.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.0
/src/acdhOeaw/arche/lib/dissCache/CachePdo.php
1
<?php
2

3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2024 zozlak.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26

27
namespace acdhOeaw\arche\lib\dissCache;
28

29
use PDO;
30

31
/**
32
 * Description of CachePdo
33
 *
34
 * @author zozlak
35
 */
36
class CachePdo implements CacheInterface {
37

38
    private PDO $pdo;
39
    private string $lockPath;
40
    private string $driver;
41

42
    public function __construct(string $connString, ?string $cacheId = null) {
43
        $this->driver = strtolower(preg_replace('/:.*$/', '', $connString));
9✔
44
        if ($this->driver !== 'sqlite') {
9✔
45
            throw new \RuntimeException("Database driver $this->driver not supported");
×
46
        }
47
        $this->pdo = new PDO($connString);
9✔
48
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
9✔
49

50
        $this->lockPath = sys_get_temp_dir() . '/cachePdo_' . ($cacheId ?? hash('xxh128', __FILE__));
9✔
51

52
        $this->maintainDb();
9✔
53
    }
54

55
    public function get(string $key): CacheItem | false {
56
        $query = $this->pdo->prepare("
9✔
57
            SELECT id, created, value
58
            FROM vals v
59
            WHERE EXISTS (SELECT 1 FROM keys WHERE key = ? AND v.id = id)
60
        ");
9✔
61
        $query->execute([$key]);
9✔
62
        return $query->fetchObject(CacheItem::class);
9✔
63
    }
64

65
    public function set(array $keys, string $value, ?int $id): int {
66
        $query = $this->pdo->prepare("
9✔
67
            INSERT OR REPLACE INTO vals (id, created, value) 
68
            VALUES (?, current_timestamp, ?)
69
            RETURNING id
70
        ");
9✔
71
        $query->execute([$id, $value]);
9✔
72
        $id    = $query->fetchColumn();
9✔
73

74
        $query = $this->pdo->prepare("INSERT OR REPLACE INTO keys (key, id) VALUES (?, ?)");
9✔
75
        foreach ($keys as $i) {
9✔
76
            $query->execute([$i, $id]);
9✔
77
        }
78

79
        return (int) $id;
9✔
80
    }
81

82
    private function maintainDb(): void {
83
        if (!file_exists($this->lockPath)) {
9✔
84
            $this->pdo->query("
9✔
85
                CREATE TABLE IF NOT EXISTS keys (
86
                    key text primary key not null,
87
                    id integer
88
                )
89
            ");
9✔
90

91
            $this->pdo->query("
9✔
92
                CREATE TABLE IF NOT EXISTS vals (
93
                    id integer primary key not null,
94
                    created timestamp not null,
95
                    value text not null
96
                )
97
            ");
9✔
98

99
            file_put_contents($this->lockPath, "");
9✔
100
        }
101
    }
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc