Completed
Pull Request — master (#244)
by Marcel
03:12
created

Xhgui_Searcher_Pdo::getAll()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 8.829
c 0
b 0
f 0
cc 2
nc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        if (false === $row = $stmt->fetch(PDO::FETCH_ASSOC)) {
55
            throw new Exception('No profile available yet.');
56
        }
57
58
        return new Xhgui_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 Xhgui_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 = array())
128
    {
129
        // TODO: Implement getForUrl() method.
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function getPercentileForUrl($percentile, $url, $search = array())
136
    {
137
        // TODO: Implement getPercentileForUrl() method.
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getAvgsForUrl($url, $search = array())
144
    {
145
        // TODO: Implement getAvgsForUrl() method.
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function getAll($options = array())
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 = $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...
156
        $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...
157
158
        $stmt = $this->pdo->query("
159
          SELECT
160
            id,
161
            url,
162
            SERVER,
163
            GET,
164
            ENV,
165
            simple_url,
166
            request_ts,
167
            request_ts_micro,
168
            request_date,
169
            main_wt,
170
            main_ct,
171
            main_cpu, 
172
            main_mu,
173
            main_pmu
174
          FROM {$this->table}
175
          ORDER BY request_ts DESC
176
        ", PDO::FETCH_ASSOC);
177
178
        $results = [];
179
        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
180
            $results[] = new Xhgui_Profile([
181
                '_id' => $row['id'],
182
                'meta' => [
183
                    'url' => $row['url'],
184
                    'SERVER' => json_decode($row['SERVER'], true),
185
                    'get' => json_decode($row['GET'], true),
186
                    'env' => json_decode($row['ENV'], true),
187
                    'simple_url' => $row['simple_url'],
188
                    'request_ts' => $row['request_ts'],
189
                    'request_ts_micro' => $row['request_ts_micro'],
190
                    'request_date' => $row['request_date'],
191
                ],
192
                'profile' => [
193
                    'main()' => [
194
                        'wt' => (int) $row['main_wt'],
195
                        'ct' => (int) $row['main_ct'],
196
                        'cpu' => (int) $row['main_cpu'],
197
                        'mu' => (int) $row['main_mu'],
198
                        'pmu' => (int) $row['main_pmu'],
199
                    ]
200
                ]
201
            ]);
202
        }
203
204
        return array(
205
            'results' => $results,
206
            'sort' => 'meta.request_ts',
207
            'direction' => 'desc',
208
            'page' => 1,
209
            'perPage' => count($results),
210
            'totalPages' => 1
211
        );
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217
    public function insert($profile)
218
    {
219
        $this->saver->save($profile);
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function delete($id)
226
    {
227
        $stmt = $this->pdo->prepare("
228
          DELETE FROM {$this->table}
229
          WHERE id = :id
230
        ");
231
232
        $stmt->execute(['id' => $id]);
233
    }
234
235
    /**
236
     * {@inheritdoc}
237
     */
238
    public function truncate()
239
    {
240
        return is_int(
241
            $this->pdo->exec("DROP TABLE IF EXISTS {$this->table}")
242
        );
243
    }
244
}
245