Completed
Pull Request — master (#244)
by Marcel
01:35
created

Xhgui_Searcher_Pdo::insert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 Xhgui_Saver_Pdo
7
     */
8
    private $saver;
9
10
    /**
11
     * @var PDO
12
     */
13
    private $pdo;
14
15
    /**
16
     * @var string
17
     */
18
    private $table;
19
20
    /**
21
     * @param Xhgui_Saver_Pdo $saver
22
     * @param PDO             $pdo   An open database connection
23
     * @param string          $table Table name where Xhgui profiles are stored
24
     */
25
    public function __construct(Xhgui_Saver_Pdo $saver, PDO $pdo, $table)
26
    {
27
        $this->saver = $saver;
28
        $this->pdo = $pdo;
29
        $this->table = $table;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 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...
36
    {
37
        $stmt = $this->pdo->query("
38
          SELECT
39
            id,
40
            profile,
41
            url,
42
            SERVER,
43
            GET,
44
            ENV,
45
            simple_url,
46
            request_ts,
47
            request_ts_micro,
48
            request_date
49
          FROM {$this->table}
50
          ORDER BY request_date ASC
51
          LIMIT 1
52
        ");
53
54
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
55
        if (false === $row) {
56
            throw new Exception('No profile available yet.');
57
        }
58
59
        return new Xhgui_Profile([
60
            '_id' => $row['id'],
61
            'meta' => [
62
                'url' => $row['url'],
63
                'SERVER' => json_decode($row['SERVER'], true),
64
                'get' => json_decode($row['GET'], true),
65
                'env' => json_decode($row['ENV'], true),
66
                'simple_url' => $row['simple_url'],
67
                'request_ts' => (int) $row['request_ts'],
68
                'request_ts_micro' => $row['request_ts_micro'],
69
                'request_date' => $row['request_date'],
70
            ],
71
            'profile' => json_decode($row['profile'], true)
72
        ]);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function query($conditions, $limit, $fields = [])
79
    {
80
        // TODO: Implement query() method.
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 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...
87
    {
88
        $stmt = $this->pdo->prepare("
89
          SELECT
90
            profile,
91
            url,
92
            SERVER,
93
            GET,
94
            ENV,
95
            simple_url,
96
            request_ts,
97
            request_ts_micro,
98
            request_date
99
          FROM {$this->table}
100
          WHERE id = :id
101
        ");
102
103
        $stmt->execute(['id' => $id]);
104
105
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
106
        if (false === $row) {
107
            throw new Exception('No profile data found.');
108
        }
109
110
        return new Xhgui_Profile([
111
            '_id' => $id,
112
            'meta' => [
113
                'url' => $row['url'],
114
                'SERVER' => json_decode($row['SERVER'], true),
115
                'get' => json_decode($row['GET'], true),
116
                'env' => json_decode($row['ENV'], true),
117
                'simple_url' => $row['simple_url'],
118
                'request_ts' => (int) $row['request_ts'],
119
                'request_ts_micro' => $row['request_ts_micro'],
120
                'request_date' => $row['request_date'],
121
            ],
122
            'profile' => json_decode($row['profile'], true)
123
        ]);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getForUrl($url, $options, $conditions = array())
130
    {
131
        // TODO: Implement getForUrl() method.
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getPercentileForUrl($percentile, $url, $search = array())
138
    {
139
        // TODO: Implement getPercentileForUrl() method.
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function getAvgsForUrl($url, $search = array())
146
    {
147
        // TODO: Implement getAvgsForUrl() method.
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function getAll($options = array())
154
    {
155
        $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...
156
        $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...
157
        $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...
158
        $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...
159
160
        $stmt = $this->pdo->query("
161
          SELECT
162
            id,
163
            url,
164
            SERVER,
165
            GET,
166
            ENV,
167
            simple_url,
168
            request_ts,
169
            request_ts_micro,
170
            request_date,
171
            main_wt,
172
            main_ct,
173
            main_cpu, 
174
            main_mu,
175
            main_pmu
176
          FROM {$this->table}
177
          ORDER BY request_ts DESC
178
        ", PDO::FETCH_ASSOC);
179
180
        $results = [];
181
        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
182
            $results[] = new Xhgui_Profile([
183
                '_id' => $row['id'],
184
                'meta' => [
185
                    'url' => $row['url'],
186
                    'SERVER' => json_decode($row['SERVER'], true),
187
                    'get' => json_decode($row['GET'], true),
188
                    'env' => json_decode($row['ENV'], true),
189
                    'simple_url' => $row['simple_url'],
190
                    'request_ts' => $row['request_ts'],
191
                    'request_ts_micro' => $row['request_ts_micro'],
192
                    'request_date' => $row['request_date'],
193
                ],
194
                'profile' => [
195
                    'main()' => [
196
                        'wt' => (int) $row['main_wt'],
197
                        'ct' => (int) $row['main_ct'],
198
                        'cpu' => (int) $row['main_cpu'],
199
                        'mu' => (int) $row['main_mu'],
200
                        'pmu' => (int) $row['main_pmu'],
201
                    ]
202
                ]
203
            ]);
204
        }
205
206
        return array(
207
            'results' => $results,
208
            'sort' => 'meta.request_ts',
209
            'direction' => 'desc',
210
            'page' => 1,
211
            'perPage' => count($results),
212
            'totalPages' => 1
213
        );
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219
    public function insert($profile)
220
    {
221
        $this->saver->save($profile);
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function delete($id)
228
    {
229
        $stmt = $this->pdo->prepare("
230
          DELETE FROM {$this->table}
231
          WHERE id = :id
232
        ");
233
234
        $stmt->execute(['id' => $id]);
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240
    public function truncate()
241
    {
242
        return is_int(
243
            $this->pdo->exec("DROP TABLE IF EXISTS {$this->table}")
244
        );
245
    }
246
}
247