Completed
Pull Request — master (#331)
by Elan
02:10 queued 57s
created

PdoSearcher::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace XHGui\Searcher;
4
5
use Exception;
6
use PDO;
7
use XHGui\Profile;
8
9
class PdoSearcher implements SearcherInterface
10
{
11
    /**
12
     * @var PDO
13
     */
14
    private $pdo;
15
16
    /**
17
     * @var string
18
     */
19
    private $table;
20
21
    /**
22
     * @param PDO    $pdo   An open database connection
23
     * @param string $table Table name where Xhgui profiles are stored
24
     */
25
    public function __construct(PDO $pdo, $table)
26
    {
27
        $this->pdo = $pdo;
28
        $this->table = $table;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 View Code Duplication
    public function latest()
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...
35
    {
36
        $stmt = $this->pdo->query("
37
          SELECT
38
            id,
39
            profile,
40
            url,
41
            SERVER,
42
            GET,
43
            ENV,
44
            simple_url,
45
            request_ts,
46
            request_ts_micro,
47
            request_date
48
          FROM {$this->table}
49
          ORDER BY request_date ASC
50
          LIMIT 1
51
        ");
52
53
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
54
        if (false === $row) {
55
            throw new Exception('No profile available yet.');
56
        }
57
58
        return new Profile([
59
            '_id' => $row['id'],
60
            'meta' => [
61
                'url' => $row['url'],
62
                'SERVER' => json_decode($row['SERVER'], true),
63
                'get' => json_decode($row['GET'], true),
64
                'env' => json_decode($row['ENV'], true),
65
                'simple_url' => $row['simple_url'],
66
                'request_ts' => (int) $row['request_ts'],
67
                'request_ts_micro' => $row['request_ts_micro'],
68
                'request_date' => $row['request_date'],
69
            ],
70
            'profile' => json_decode($row['profile'], true),
71
        ]);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function query($conditions, $limit, $fields = [])
78
    {
79
        // TODO: Implement query() method.
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 View Code Duplication
    public function get($id)
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...
86
    {
87
        $stmt = $this->pdo->prepare("
88
          SELECT
89
            profile,
90
            url,
91
            SERVER,
92
            GET,
93
            ENV,
94
            simple_url,
95
            request_ts,
96
            request_ts_micro,
97
            request_date
98
          FROM {$this->table}
99
          WHERE id = :id
100
        ");
101
102
        $stmt->execute(['id' => $id]);
103
104
        if (false === $row = $stmt->fetch(PDO::FETCH_ASSOC)) {
105
            throw new Exception('No profile data found.');
106
        }
107
108
        return new Profile([
109
            '_id' => $id,
110
            'meta' => [
111
                'url' => $row['url'],
112
                'SERVER' => json_decode($row['SERVER'], true),
113
                'get' => json_decode($row['GET'], true),
114
                'env' => json_decode($row['ENV'], true),
115
                'simple_url' => $row['simple_url'],
116
                'request_ts' => (int) $row['request_ts'],
117
                'request_ts_micro' => $row['request_ts_micro'],
118
                'request_date' => $row['request_date'],
119
            ],
120
            'profile' => json_decode($row['profile'], true),
121
        ]);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getForUrl($url, $options, $conditions = [])
128
    {
129
        // TODO: Implement getForUrl() method.
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function getPercentileForUrl($percentile, $url, $search = [])
136
    {
137
        // TODO: Implement getPercentileForUrl() method.
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getAvgsForUrl($url, $search = [])
144
    {
145
        // TODO: Implement getAvgsForUrl() method.
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function getAll($options = [])
152
    {
153
        $sort = $options['sort'];
0 ignored issues
show
Unused Code introduced by
$sort is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
154
        $direction = $options['direction'];
0 ignored issues
show
Unused Code introduced by
$direction is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
155
        $page = (int)$options['page'];
156
        if ($page < 1) {
157
            $page = 1;
158
        }
159
        $perPage = (int)$options['perPage'];
160
        $url = $options['conditions']['url'] ?? "";
161
162
        $stmt = $this->pdo->prepare("
163
          SELECT COUNT(*) AS count
164
          FROM {$this->table}
165
          WHERE simple_url LIKE :url
166
        ");
167
        $stmt->execute(['url' => '%'.$url.'%']);
168
        $totalRows = (int)$stmt->fetchColumn();
169
170
        $totalPages = max(ceil($totalRows/$perPage), 1);
171
        if ($page > $totalPages) {
172
            $page = $totalPages;
173
        }
174
        $skip = ($page-1) * $perPage;
175
176
        $stmt = $this->pdo->prepare("
177
          SELECT
178
            id,
179
            url,
180
            SERVER,
181
            GET,
182
            ENV,
183
            simple_url,
184
            request_ts,
185
            request_ts_micro,
186
            request_date,
187
            main_wt,
188
            main_ct,
189
            main_cpu,
190
            main_mu,
191
            main_pmu
192
          FROM {$this->table}
193
          WHERE simple_url LIKE :url
194
          ORDER BY request_ts DESC
195
          LIMIT $skip, $perPage
196
        ");
197
        $stmt->execute(['url' => '%'.$url.'%']);
198
199
        $results = [];
200
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
201
            $results[] = new Profile([
202
                '_id' => $row['id'],
203
                'meta' => [
204
                    'url' => $row['url'],
205
                    'SERVER' => json_decode($row['SERVER'], true),
206
                    'get' => json_decode($row['GET'], true),
207
                    'env' => json_decode($row['ENV'], true),
208
                    'simple_url' => $row['simple_url'],
209
                    'request_ts' => $row['request_ts'],
210
                    'request_ts_micro' => $row['request_ts_micro'],
211
                    'request_date' => $row['request_date'],
212
                ],
213
                'profile' => [
214
                    'main()' => [
215
                        'wt' => (int) $row['main_wt'],
216
                        'ct' => (int) $row['main_ct'],
217
                        'cpu' => (int) $row['main_cpu'],
218
                        'mu' => (int) $row['main_mu'],
219
                        'pmu' => (int) $row['main_pmu'],
220
                    ],
221
                ],
222
            ]);
223
        }
224
225
        return [
226
            'results' => $results,
227
            'sort' => 'meta.request_ts',
228
            'direction' => 'desc',
229
            'page' => $page,
230
            'perPage' => $perPage,
231
            'totalPages' => $totalPages,
232
        ];
233
    }
234
235
    /**
236
     * {@inheritdoc}
237
     */
238
    public function delete($id)
239
    {
240
        $stmt = $this->pdo->prepare("
241
          DELETE FROM {$this->table}
242
          WHERE id = :id
243
        ");
244
245
        $stmt->execute(['id' => $id]);
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251
    public function truncate()
252
    {
253
        return is_int(
254
            $this->pdo->exec("DELETE FROM {$this->table}")
255
        );
256
    }
257
258
    /**
259
     * {@inheritdoc}
260
     */
261
    public function saveWatch(array $data)
262
    {
263
        return true;
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269
    public function getAllWatches()
270
    {
271
        return [];
272
    }
273
274
    /**
275
     * {@inheritdoc}
276
     */
277
    public function truncateWatches()
278
    {
279
    }
280
281
    /**
282
     * {@inheritdoc}
283
     */
284
    public function stats()
285
    {
286
        $stmt = $this->pdo->query("
287
          SELECT
288
            COUNT(*) AS profiles,
289
            MAX(request_ts) AS latest,
290
            SUM(LENGTH(profile)) AS bytes
291
          FROM {$this->table}
292
        ", PDO::FETCH_ASSOC);
293
294
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
295
296
        if (false === $row) {
297
            $row = [
298
                'profiles' => 0,
299
                'latest'   => 0,
300
                'bytes'    => 0,
301
            ];
302
        }
303
304
        return $row;
305
    }
306
}
307