Completed
Push — master ( 98a7a2...4b25fc )
by Elan
01:09
created

Xhgui_Searcher_Pdo::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
class Xhgui_Searcher_Pdo implements Xhgui_Searcher_Interface
4
{
5
    /**
6
     * @var PDO
7
     */
8
    private $pdo;
9
10
    /**
11
     * @var string
12
     */
13
    private $table;
14
15
    /**
16
     * @param PDO    $pdo   An open database connection
17
     * @param string $table Table name where Xhgui profiles are stored
18
     */
19
    public function __construct(PDO $pdo, $table)
20
    {
21
        $this->pdo = $pdo;
22
        $this->table = $table;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 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...
29
    {
30
        $stmt = $this->pdo->query("
31
          SELECT
32
            id,
33
            profile,
34
            url,
35
            SERVER,
36
            GET,
37
            ENV,
38
            simple_url,
39
            request_ts,
40
            request_ts_micro,
41
            request_date
42
          FROM {$this->table}
43
          ORDER BY request_date ASC
44
          LIMIT 1
45
        ");
46
47
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
48
        if (false === $row) {
49
            throw new Exception('No profile available yet.');
50
        }
51
52
        return new Xhgui_Profile([
53
            '_id' => $row['id'],
54
            'meta' => [
55
                'url' => $row['url'],
56
                'SERVER' => json_decode($row['SERVER'], true),
57
                'get' => json_decode($row['GET'], true),
58
                'env' => json_decode($row['ENV'], true),
59
                'simple_url' => $row['simple_url'],
60
                'request_ts' => (int) $row['request_ts'],
61
                'request_ts_micro' => $row['request_ts_micro'],
62
                'request_date' => $row['request_date'],
63
            ],
64
            'profile' => json_decode($row['profile'], true)
65
        ]);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function query($conditions, $limit, $fields = [])
72
    {
73
        // TODO: Implement query() method.
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 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...
80
    {
81
        $stmt = $this->pdo->prepare("
82
          SELECT
83
            profile,
84
            url,
85
            SERVER,
86
            GET,
87
            ENV,
88
            simple_url,
89
            request_ts,
90
            request_ts_micro,
91
            request_date
92
          FROM {$this->table}
93
          WHERE id = :id
94
        ");
95
96
        $stmt->execute(['id' => $id]);
97
98
        if (false === $row = $stmt->fetch(PDO::FETCH_ASSOC)) {
99
            throw new Exception('No profile data found.');
100
        }
101
102
        return new Xhgui_Profile([
103
            '_id' => $id,
104
            'meta' => [
105
                'url' => $row['url'],
106
                'SERVER' => json_decode($row['SERVER'], true),
107
                'get' => json_decode($row['GET'], true),
108
                'env' => json_decode($row['ENV'], true),
109
                'simple_url' => $row['simple_url'],
110
                'request_ts' => (int) $row['request_ts'],
111
                'request_ts_micro' => $row['request_ts_micro'],
112
                'request_date' => $row['request_date'],
113
            ],
114
            'profile' => json_decode($row['profile'], true)
115
        ]);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getForUrl($url, $options, $conditions = array())
122
    {
123
        // TODO: Implement getForUrl() method.
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getPercentileForUrl($percentile, $url, $search = array())
130
    {
131
        // TODO: Implement getPercentileForUrl() method.
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getAvgsForUrl($url, $search = array())
138
    {
139
        // TODO: Implement getAvgsForUrl() method.
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function getAll($options = array())
146
    {
147
        $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...
148
        $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...
149
        $page = (int)$options['page'];
150
        if ($page < 1) {
151
            $page = 1;
152
        }
153
        $perPage = (int)$options['perPage'];
154
        $url = $options['conditions']['url'] ?? "";
155
156
        $stmt = $this->pdo->prepare("
157
          SELECT COUNT(*) AS count
158
          FROM {$this->table}
159
          WHERE simple_url LIKE :url
160
        ");
161
        $stmt->execute(['url' => '%'.$url.'%']);
162
        $totalRows = (int)$stmt->fetchColumn();
163
164
        $totalPages = max(ceil($totalRows/$perPage), 1);
165
        if ($page > $totalPages) {
166
            $page = $totalPages;
167
        }
168
        $skip = ($page-1) * $perPage;
169
170
        $stmt = $this->pdo->prepare("
171
          SELECT
172
            id,
173
            url,
174
            SERVER,
175
            GET,
176
            ENV,
177
            simple_url,
178
            request_ts,
179
            request_ts_micro,
180
            request_date,
181
            main_wt,
182
            main_ct,
183
            main_cpu,
184
            main_mu,
185
            main_pmu
186
          FROM {$this->table}
187
          WHERE simple_url LIKE :url
188
          ORDER BY request_ts DESC
189
          LIMIT $skip, $perPage
190
        ");
191
        $stmt->execute(['url' => '%'.$url.'%']);
192
193
        $results = [];
194
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
195
            $results[] = new Xhgui_Profile([
196
                '_id' => $row['id'],
197
                'meta' => [
198
                    'url' => $row['url'],
199
                    'SERVER' => json_decode($row['SERVER'], true),
200
                    'get' => json_decode($row['GET'], true),
201
                    'env' => json_decode($row['ENV'], true),
202
                    'simple_url' => $row['simple_url'],
203
                    'request_ts' => $row['request_ts'],
204
                    'request_ts_micro' => $row['request_ts_micro'],
205
                    'request_date' => $row['request_date'],
206
                ],
207
                'profile' => [
208
                    'main()' => [
209
                        'wt' => (int) $row['main_wt'],
210
                        'ct' => (int) $row['main_ct'],
211
                        'cpu' => (int) $row['main_cpu'],
212
                        'mu' => (int) $row['main_mu'],
213
                        'pmu' => (int) $row['main_pmu'],
214
                    ]
215
                ]
216
            ]);
217
        }
218
219
        return array(
220
            'results' => $results,
221
            'sort' => 'meta.request_ts',
222
            'direction' => 'desc',
223
            'page' => $page,
224
            'perPage' => $perPage,
225
            'totalPages' => $totalPages,
226
        );
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     */
232
    public function delete($id)
233
    {
234
        $stmt = $this->pdo->prepare("
235
          DELETE FROM {$this->table}
236
          WHERE id = :id
237
        ");
238
239
        $stmt->execute(['id' => $id]);
240
    }
241
242
    /**
243
     * {@inheritdoc}
244
     */
245
    public function truncate()
246
    {
247
        return is_int(
248
            $this->pdo->exec("DELETE FROM {$this->table}")
249
        );
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255
    public function saveWatch(array $data)
256
    {
257
        return true;
258
    }
259
260
    /**
261
     * {@inheritdoc}
262
     */
263
    public function getAllWatches()
264
    {
265
        return [];
266
    }
267
268
    /**
269
     * {@inheritdoc}
270
     */
271
    public function truncateWatches()
272
    {
273
    }
274
275
    /**
276
     * {@inheritdoc}
277
     */
278
    public function stats()
279
    {
280
        $stmt = $this->pdo->query("
281
          SELECT
282
            COUNT(*) AS profiles,
283
            MAX(request_ts) AS latest,
284
            SUM(LENGTH(profile)) AS bytes
285
          FROM {$this->table}
286
        ", PDO::FETCH_ASSOC);
287
288
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
289
290
        if (false === $row) {
291
            $row = [
292
                'profiles' => 0,
293
                'latest'   => 0,
294
                'bytes'    => 0,
295
            ];
296
        }
297
298
        return $row;
299
    }
300
}
301