Completed
Push — master ( 9e7a87...5357d0 )
by Elan
01:21 queued 10s
created

PdoRepository::getById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
dl 26
loc 26
rs 9.504
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace XHGui\Db;
4
5
use Generator;
6
use PDO;
7
use RuntimeException;
8
9
class PdoRepository
10
{
11
    /** @var PDO */
12
    private $pdo;
13
14
    /** @var string */
15
    private $table;
16
17
    /**
18
     * @param PDO $pdo An open database connection
19
     * @param string $table Table name where Xhgui profiles are stored
20
     */
21
    public function __construct(PDO $pdo, string $table)
22
    {
23
        $this->pdo = $pdo;
24
        $this->table = sprintf('"%s"', $table);
25
    }
26
27 View Code Duplication
    public function getLatest(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        $query = sprintf('
30
          SELECT
31
            "id",
32
            "profile",
33
            "url",
34
            "SERVER",
35
            "GET",
36
            "ENV",
37
            "simple_url",
38
            "request_ts",
39
            "request_ts_micro,"
40
            "request_date"
41
          FROM %s
42
          ORDER BY "request_date" ASC
43
          LIMIT 1',
44
            $this->table
45
        );
46
        $stmt = $this->pdo->query($query);
47
48
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
49
        if ($row === false) {
50
            throw new RuntimeException('No profile available yet.');
51
        }
52
53
        return $row;
54
    }
55
56 View Code Duplication
    public function getById(string $id): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        $query = sprintf('
59
          SELECT
60
            "profile",
61
            "url",
62
            "SERVER",
63
            "GET",
64
            "ENV",
65
            "simple_url",
66
            "request_ts",
67
            "request_ts_micro",
68
            "request_date"
69
          FROM %s
70
          WHERE id = :id
71
        ', $this->table);
72
        $stmt = $this->pdo->prepare($query);
73
        $stmt->execute(['id' => $id]);
74
75
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
76
        if ($row === false) {
77
            throw new RuntimeException('No profile data found.');
78
        }
79
80
        return $row;
81
    }
82
83
    public function countByUrl(string $url): int
84
    {
85
        $query = sprintf('
86
          SELECT COUNT(*) AS count
87
          FROM %s
88
          WHERE "simple_url" LIKE :url
89
        ', $this->table);
90
        $stmt = $this->pdo->prepare($query);
91
        $stmt->execute(['url' => '%' . $url . '%']);
92
93
        return (int)$stmt->fetchColumn();
94
    }
95
96
    public function findByUrl(string $url, string $direction, int $skip, int $perPage): Generator
97
    {
98
        $query = sprintf('
99
          SELECT
100
            "id",
101
            "url",
102
            "SERVER",
103
            "GET",
104
            "ENV",
105
            "simple_url",
106
            "request_ts",
107
            "request_ts_micro",
108
            "request_date",
109
            "main_wt",
110
            "main_ct",
111
            "main_cpu",
112
            "main_mu",
113
            "main_pmu"
114
          FROM %s
115
          WHERE "simple_url" LIKE :url
116
          ORDER BY "request_ts" %s
117
          LIMIT %d OFFSET %d',
118
            $this->table,
119
            $direction,
120
            $skip,
121
            $perPage
122
        );
123
        $stmt = $this->pdo->prepare($query);
124
        $stmt->execute(['url' => '%' . $url . '%']);
125
126
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
127
            yield $row;
128
        }
129
    }
130
131
    public function deleteById(string $id)
132
    {
133
        $stmt = $this->pdo->prepare(sprintf('
134
          DELETE FROM %s
135
          WHERE id = :id
136
        ', $this->table));
137
138
        $stmt->execute(['id' => $id]);
139
    }
140
141
    public function deleteAll()
142
    {
143
        return is_int(
144
            $this->pdo->exec(sprintf('DELETE FROM %s', $this->table))
145
        );
146
    }
147
148
    public function getStatistics()
149
    {
150
        $stmt = $this->pdo->query(
151
            sprintf(
152
                '
153
          SELECT
154
            COUNT(*) AS profiles,
155
            MAX("request_ts") AS latest,
156
            SUM(LENGTH("profile")) AS bytes
157
          FROM %s',
158
                $this->table
159
            ),
160
            PDO::FETCH_ASSOC
161
        );
162
163
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
164
165
        return $row ?: null;
166
    }
167
168
    public function initSchema()
169
    {
170
        $this->pdo->exec(sprintf('
171
            CREATE TABLE IF NOT EXISTS %s (
172
              "id"               CHAR(24) PRIMARY KEY,
173
              "profile"          TEXT           NOT NULL,
174
              "url"              TEXT           NULL,
175
              "SERVER"           TEXT           NULL,
176
              "GET"              TEXT           NULL,
177
              "ENV"              TEXT           NULL,
178
              "simple_url"       TEXT           NULL,
179
              "request_ts"       INTEGER        NOT NULL,
180
              "request_ts_micro" NUMERIC(15, 4) NOT NULL,
181
              "request_date"     DATE           NOT NULL,
182
              "main_wt"          INTEGER        NOT NULL,
183
              "main_ct"          INTEGER        NOT NULL,
184
              "main_cpu"         INTEGER        NOT NULL,
185
              "main_mu"          INTEGER        NOT NULL,
186
              "main_pmu"         INTEGER        NOT NULL
187
            )
188
        ', $this->table));
189
    }
190
191
    public function saveProfile(array $data)
192
    {
193
        $stmt = $this->pdo->prepare(sprintf('
194
            INSERT INTO %s (
195
              "id",
196
              "profile",
197
              "url",
198
              "SERVER",
199
              "GET",
200
              "ENV",
201
              "simple_url",
202
              "request_ts",
203
              "request_ts_micro",
204
              "request_date",
205
              "main_wt",
206
              "main_ct",
207
              "main_cpu",
208
              "main_mu",
209
              "main_pmu"
210
            ) VALUES (
211
              :id,
212
              :profile,
213
              :url,
214
              :SERVER,
215
              :GET,
216
              :ENV,
217
              :simple_url,
218
              :request_ts,
219
              :request_ts_micro,
220
              :request_date,
221
              :main_wt,
222
              :main_ct,
223
              :main_cpu,
224
              :main_mu,
225
              :main_pmu
226
            )
227
        ', $this->table));
228
        $stmt->execute($data);
229
    }
230
}
231