PdoSearcher   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 189
Duplicated Lines 20.11 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 38
loc 189
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 49 3
A delete() 0 4 1
A truncate() 0 6 1
A saveWatch() 0 4 1
A getAllWatches() 0 4 1
A truncateWatches() 0 4 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\Exception\NotImplementedException;
7
use XHGui\Options\SearchOptions;
8
use XHGui\Profile;
9
10
class PdoSearcher implements SearcherInterface
11
{
12
    /** @var PdoRepository */
13
    private $db;
14
15
    public function __construct(PdoRepository $db)
16
    {
17
        $this->db = $db;
18
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 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...
24
    {
25
        $row = $this->db->getLatest();
26
27
        return new Profile([
28
            '_id' => $row['id'],
29
            'meta' => [
30
                'url' => $row['url'],
31
                'SERVER' => json_decode($row['SERVER'], true),
32
                'get' => json_decode($row['GET'], true),
33
                'env' => json_decode($row['ENV'], true),
34
                'simple_url' => $row['simple_url'],
35
                'request_ts' => (int) $row['request_ts'],
36
                'request_ts_micro' => $row['request_ts_micro'],
37
                'request_date' => $row['request_date'],
38
            ],
39
            'profile' => json_decode($row['profile'], true),
40
        ]);
41
    }
42
43
    public function query($conditions, $limit, $fields = []): void
44
    {
45
        throw NotImplementedException::notImplementedPdo(__METHOD__);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 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...
52
    {
53
        $row = $this->db->getById($id);
54
55
        return new Profile([
56
            '_id' => $id,
57
            'meta' => [
58
                'url' => $row['url'],
59
                'SERVER' => json_decode($row['SERVER'], true),
60
                'get' => json_decode($row['GET'], true),
61
                'env' => json_decode($row['ENV'], true),
62
                'simple_url' => $row['simple_url'],
63
                'request_ts' => (int) $row['request_ts'],
64
                'request_ts_micro' => $row['request_ts_micro'],
65
                'request_date' => $row['request_date'],
66
            ],
67
            'profile' => json_decode($row['profile'], true),
68
        ]);
69
    }
70
71
    public function getForUrl($url, $options, $conditions = []): void
72
    {
73
        throw NotImplementedException::notImplementedPdo(__METHOD__);
74
    }
75
76
    public function getPercentileForUrl($percentile, $url, $search = []): void
77
    {
78
        throw NotImplementedException::notImplementedPdo(__METHOD__);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getAvgsForUrl($url, $search = []): void
85
    {
86
        throw NotImplementedException::notImplementedPdo(__METHOD__);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getAll(SearchOptions $options): array
93
    {
94
        $page = $options['page'];
95
        $direction = $options['direction'];
96
        $perPage = $options['perPage'];
97
        $url = $options['conditions']['url'] ?? '';
98
99
        $totalRows = $this->db->countByUrl($url);
100
        $totalPages = max(ceil($totalRows / $perPage), 1);
101
        if ($page > $totalPages) {
102
            $page = $totalPages;
103
        }
104
        $skip = ($page - 1) * $perPage;
105
106
        $results = [];
107
        foreach ($this->db->findByUrl($url, $direction, $skip, $perPage) as $row) {
108
            $results[] = new Profile([
109
                '_id' => $row['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' => $row['request_ts'],
117
                    'request_ts_micro' => $row['request_ts_micro'],
118
                    'request_date' => $row['request_date'],
119
                ],
120
                'profile' => [
121
                    'main()' => [
122
                        'wt' => (int) $row['main_wt'],
123
                        'ct' => (int) $row['main_ct'],
124
                        'cpu' => (int) $row['main_cpu'],
125
                        'mu' => (int) $row['main_mu'],
126
                        'pmu' => (int) $row['main_pmu'],
127
                    ],
128
                ],
129
            ]);
130
        }
131
132
        return [
133
            'results' => $results,
134
            'sort' => 'meta.request_ts',
135
            'direction' => $direction,
136
            'page' => $page,
137
            'perPage' => $perPage,
138
            'totalPages' => $totalPages,
139
        ];
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function delete($id): void
146
    {
147
        $this->db->deleteById($id);
148
    }
149
150
    public function truncate()
151
    {
152
        $this->db->deleteAll();
153
154
        return $this;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function saveWatch(array $data)
161
    {
162
        return true;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function getAllWatches()
169
    {
170
        return [];
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function truncateWatches()
177
    {
178
        return $this;
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function stats()
185
    {
186
        $row = $this->db->getStatistics();
187
188
        if (!$row) {
189
            $row = [
190
                'profiles' => 0,
191
                'latest' => 0,
192
                'bytes' => 0,
193
            ];
194
        }
195
196
        return $row;
197
    }
198
}
199