Completed
Push — master ( 0765b1...3d50cd )
by Seth
03:43 queued 01:43
created

LibGuidesSearch::search()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 1
1
<?php
2
3
namespace smtech\StMarksSearch\LibApps\LibGuides;
4
5
use Exception;
6
use smtech\StMarksSearch\Relevance;
7
use smtech\StMarksSearch\SearchResult;
8
use smtech\StMarksSearch\SearchSource;
9
use smtech\StMarksSearch\LibApps\AbstractLibAppsSearchDomain;
10
11
class LibGuidesSearch extends AbstractLibAppsSearchDomain
12
{
13
    public function search($query)
14
    {
15
        $source = new SearchSource($this);
16
        $results = [];
17
        foreach ($this->getApi()->get(
18
            '/guides',
19
            [
20
                'search_terms' => $query,
21
                'search_match' => 2
22
            ]
23
        ) as $result) {
24
            $results[] = new SearchResult(
25
                $result['url'],
26
                $this->relevance($result, $query),
27
                $result['name'],
28
                $result['description'],
29
                $source
30
            );
31
        }
32
33
        $this->sortByRelevance($results);
34
        return $results;
35
    }
36
37 View Code Duplication
    protected function relevance($guide, $query)
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...
38
    {
39
        $relevance = new Relevance();
40
41
        $relevance->add(Relevance::stringProportion($guide['name'], $query), 'title match');
42
43
        if (($count = substr_count(strtolower($guide['description']), strtolower($query))) > 0) {
44
            $relevance->add($count * 0.01, "$count occurrences in description");
45
        }
46
47
        return $relevance;
48
    }
49
}
50