Completed
Pull Request — master (#244)
by Marcel
02:50
created

Xhgui_Searcher_Pdo::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 39

Duplication

Lines 39
Ratio 100 %

Importance

Changes 0
Metric Value
dl 39
loc 39
rs 9.296
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
class Xhgui_Searcher_Pdo implements Xhgui_Searcher_Interface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
99
        if (false === $row) {
100
            throw new Exception('No profile data found.');
101
        }
102
103
        return new Xhgui_Profile([
104
            '_id' => $id,
105
            'meta' => [
106
                'url' => $row['url'],
107
                'SERVER' => json_decode($row['SERVER'], true),
108
                'get' => json_decode($row['GET'], true),
109
                'env' => json_decode($row['ENV'], true),
110
                'simple_url' => $row['simple_url'],
111
                'request_ts' => (int) $row['request_ts'],
112
                'request_ts_micro' => $row['request_ts_micro'],
113
                'request_date' => $row['request_date'],
114
            ],
115
            'profile' => json_decode($row['profile'], true)
116
        ]);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getForUrl($url, $options, $conditions = array())
123
    {
124
        // TODO: Implement getForUrl() method.
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function getPercentileForUrl($percentile, $url, $search = array())
131
    {
132
        // TODO: Implement getPercentileForUrl() method.
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function getAvgsForUrl($url, $search = array())
139
    {
140
        // TODO: Implement getAvgsForUrl() method.
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function getAll($options = array())
147
    {
148
        $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...
149
        $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...
150
        $page = $options['page'];
0 ignored issues
show
Unused Code introduced by
$page 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...
151
        $perPage = $options['perPage'];
0 ignored issues
show
Unused Code introduced by
$perPage 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...
152
153
        $stmt = $this->pdo->query("
154
          SELECT
155
            id,
156
            url,
157
            SERVER,
158
            GET,
159
            ENV,
160
            simple_url,
161
            request_ts,
162
            request_ts_micro,
163
            request_date,
164
            main_wt,
165
            main_ct,
166
            main_cpu, 
167
            main_mu,
168
            main_pmu
169
          FROM {$this->table}
170
          ORDER BY request_ts DESC
171
        ", PDO::FETCH_ASSOC);
172
173
        $results = [];
174
        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
175
            $results[] = new Xhgui_Profile([
176
                '_id' => $row['id'],
177
                'meta' => [
178
                    'url' => $row['url'],
179
                    'SERVER' => json_decode($row['SERVER'], true),
180
                    'get' => json_decode($row['GET'], true),
181
                    'env' => json_decode($row['ENV'], true),
182
                    'simple_url' => $row['simple_url'],
183
                    'request_ts' => $row['request_ts'],
184
                    'request_ts_micro' => $row['request_ts_micro'],
185
                    'request_date' => $row['request_date'],
186
                ],
187
                'profile' => [
188
                    'main()' => [
189
                        'wt' => (int) $row['main_wt'],
190
                        'ct' => (int) $row['main_ct'],
191
                        'cpu' => (int) $row['main_cpu'],
192
                        'mu' => (int) $row['main_mu'],
193
                        'pmu' => (int) $row['main_pmu'],
194
                    ]
195
                ]
196
            ]);
197
        }
198
199
        return array(
200
            'results' => $results,
201
            'sort' => 'meta.request_ts',
202
            'direction' => 'desc',
203
            'page' => 1,
204
            'perPage' => count($results),
205
            'totalPages' => 1
206
        );
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function delete($id)
213
    {
214
        $stmt = $this->pdo->prepare("
215
          DELETE FROM {$this->table}
216
          WHERE id = :id
217
        ");
218
219
        $stmt->execute(['id' => $id]);
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function truncate()
226
    {
227
        return is_int(
228
            $this->pdo->exec("DROP TABLE IF EXISTS {$this->table}")
229
        );
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235
    public function saveWatch(array $data)
236
    {
237
    }
238
239
    /**
240
     * {@inheritdoc}
241
     */
242
    public function getAllWatches()
243
    {
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249
    public function truncateWatches()
250
    {
251
    }
252
}
253