Completed
Push — master ( 9e7a87...5357d0 )
by Elan
01:21 queued 10s
created

PdoSearcher   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 200
Duplicated Lines 19 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 38
loc 200
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A latest() 19 19 1
A query() 0 4 1
A get() 19 19 1
A getForUrl() 0 4 1
A getPercentileForUrl() 0 4 1
A getAvgsForUrl() 0 4 1
A getAll() 0 51 4
A delete() 0 4 1
A truncate() 0 4 1
A saveWatch() 0 4 1
A getAllWatches() 0 4 1
A truncateWatches() 0 3 1
A stats() 0 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace XHGui\Searcher;
4
5
use XHGui\Db\PdoRepository;
6
use XHGui\Profile;
7
8
class PdoSearcher implements SearcherInterface
9
{
10
    /** @var PdoRepository */
11
    private $db;
12
13
    public function __construct(PdoRepository $db)
14
    {
15
        $this->db = $db;
16
    }
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 View Code Duplication
    public function latest(): Profile
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...
22
    {
23
        $row = $this->db->getLatest();
24
25
        return new Profile([
26
            '_id' => $row['id'],
27
            'meta' => [
28
                'url' => $row['url'],
29
                'SERVER' => json_decode($row['SERVER'], true),
30
                'get' => json_decode($row['GET'], true),
31
                'env' => json_decode($row['ENV'], true),
32
                'simple_url' => $row['simple_url'],
33
                'request_ts' => (int) $row['request_ts'],
34
                'request_ts_micro' => $row['request_ts_micro'],
35
                'request_date' => $row['request_date'],
36
            ],
37
            'profile' => json_decode($row['profile'], true),
38
        ]);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function query($conditions, $limit, $fields = [])
45
    {
46
        // TODO: Implement query() method.
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 View Code Duplication
    public function get($id): Profile
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...
53
    {
54
        $row = $this->db->getById($id);
55
56
        return new Profile([
57
            '_id' => $id,
58
            'meta' => [
59
                'url' => $row['url'],
60
                'SERVER' => json_decode($row['SERVER'], true),
61
                'get' => json_decode($row['GET'], true),
62
                'env' => json_decode($row['ENV'], true),
63
                'simple_url' => $row['simple_url'],
64
                'request_ts' => (int) $row['request_ts'],
65
                'request_ts_micro' => $row['request_ts_micro'],
66
                'request_date' => $row['request_date'],
67
            ],
68
            'profile' => json_decode($row['profile'], true),
69
        ]);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getForUrl($url, $options, $conditions = [])
76
    {
77
        // TODO: Implement getForUrl() method.
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getPercentileForUrl($percentile, $url, $search = [])
84
    {
85
        // TODO: Implement getPercentileForUrl() method.
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getAvgsForUrl($url, $search = [])
92
    {
93
        // TODO: Implement getAvgsForUrl() method.
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getAll($options = [])
100
    {
101
        $page = (int)$options['page'];
102
        if ($page < 1) {
103
            $page = 1;
104
        }
105
        $perPage = (int)$options['perPage'];
106
        $url = $options['conditions']['url'] ?? "";
107
108
        $totalRows = $this->db->countByUrl($url);
109
        $totalPages = max(ceil($totalRows/$perPage), 1);
110
        if ($page > $totalPages) {
111
            $page = $totalPages;
112
        }
113
        $skip = ($page-1) * $perPage;
114
115
        $results = [];
116
        foreach ($this->db->findByUrl($url, $direction, $skip, $perPage) as $row) {
0 ignored issues
show
Bug introduced by
The variable $direction does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
117
            $results[] = new Profile([
118
                '_id' => $row['id'],
119
                'meta' => [
120
                    'url' => $row['url'],
121
                    'SERVER' => json_decode($row['SERVER'], true),
122
                    'get' => json_decode($row['GET'], true),
123
                    'env' => json_decode($row['ENV'], true),
124
                    'simple_url' => $row['simple_url'],
125
                    'request_ts' => $row['request_ts'],
126
                    'request_ts_micro' => $row['request_ts_micro'],
127
                    'request_date' => $row['request_date'],
128
                ],
129
                'profile' => [
130
                    'main()' => [
131
                        'wt' => (int) $row['main_wt'],
132
                        'ct' => (int) $row['main_ct'],
133
                        'cpu' => (int) $row['main_cpu'],
134
                        'mu' => (int) $row['main_mu'],
135
                        'pmu' => (int) $row['main_pmu'],
136
                    ],
137
                ],
138
            ]);
139
        }
140
141
        return [
142
            'results' => $results,
143
            'sort' => 'meta.request_ts',
144
            'direction' => $direction,
145
            'page' => $page,
146
            'perPage' => $perPage,
147
            'totalPages' => $totalPages,
148
        ];
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function delete($id)
155
    {
156
        $this->db->deleteById($id);
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function truncate()
163
    {
164
        return $this->db->deleteAll();
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function saveWatch(array $data)
171
    {
172
        return true;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function getAllWatches()
179
    {
180
        return [];
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function truncateWatches()
187
    {
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function stats()
194
    {
195
        $row = $this->db->getStatistics();
196
197
        if (!$row) {
198
            $row = [
199
                'profiles' => 0,
200
                'latest'   => 0,
201
                'bytes'    => 0,
202
            ];
203
        }
204
205
        return $row;
206
    }
207
}
208