Completed
Push — master ( 734eba...03e6ad )
by
unknown
02:56
created

TopSearchWidget::setStartDate()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 5
nop 1
1
<?php
2
3
namespace Charcoal\Admin\Widget\Search;
4
5
use \PDO;
6
7
use \Charcoal\Search\SearchLog;
8
9
/**
10
 * Search widget to show the latest searched-for terms and their results.
11
 */
12 View Code Duplication
class TopSearchWidget extends AbstractSearchHistoryWidget
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    /**
15
     * Load the top searches from the source.
16
     *
17
     * @return SearchLog[]
18
     */
19
    public function loadSearchHistory()
20
    {
21
        $proto  = $this->modelFactory()->get(SearchLog::class);
22
        $source = $proto->source();
23
        $table  = $source->table();
24
25
        $q = '
26
        SELECT
27
            `keyword`,
28
            COUNT(`keyword`) as num_searches,
29
            SUM(`num_results`) as num_results
30
        FROM
31
            `'.$table.'`
32
        WHERE
33
            `ts` > :start
34
        AND
35
            `ts` <= :end
36
        GROUP BY
37
            `keyword`
38
        ORDER BY
39
            num_searches DESC
40
        LIMIT
41
            20
42
        ';
43
44
        $sth = $source->dbQuery($q, [
45
            'start' => $this->startDate()->format('Y-m-d H:i:s'),
46
            'end'   => $this->endDate()->format('Y-m-d H:i:s')
47
        ]);
48
49
        $results = $sth->fetchAll(PDO::FETCH_ASSOC);
50
51
        return $results;
52
    }
53
}
54