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

NoResultsSearchWidget::loadSearchHistory()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 11

Duplication

Lines 36
Ratio 100 %

Importance

Changes 0
Metric Value
dl 36
loc 36
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
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 that produced no results.
11
 */
12 View Code Duplication
class NoResultsSearchWidget 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, that produced no results.
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
            `num_results` = 0
34
        AND
35
            `ts` > :start
36
        AND
37
            `ts` <= :end
38
        GROUP BY
39
            `keyword`
40
        ORDER BY
41
            num_searches DESC
42
        LIMIT
43
            20
44
        ';
45
46
        $sth = $source->dbQuery($q, [
47
            'start' => $this->startDate()->format('Y-m-d H:i:s'),
48
            'end'   => $this->endDate()->format('Y-m-d H:i:s')
49
        ]);
50
51
        $results = $sth->fetchAll(PDO::FETCH_ASSOC);
52
53
        return $results;
54
    }
55
}
56