Completed
Push — master ( a8f20d...0172d7 )
by Seth
01:51
created

SearchEngine   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 75
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addDomain() 0 7 1
A search() 0 12 2
B getDomains() 0 20 5
1
<?php
2
3
namespace smtech\StMarksSearch;
4
5
use smtech\StMarksSearch\SearchResult;
6
7
/**
8
 * A collection of search domains that may be searched in aggregate
9
 *
10
 * @author Seth Battis <[email protected]>
11
 */
12
class SearchEngine extends AbstractSearchDomain
13
{
14
    /**
15
     * The aggregated search domains
16
     * @var AbstractSearchDomain[]
17
     */
18
    private $searchDomains = [];
19
20
    /**
21
     * Add a new search domain to this aggregate engine
22
     * @param AbstractSearchDomain $domain
23
     */
24
    public function addDomain(AbstractSearchDomain $domain)
25
    {
26
        /*
27
         * TODO ensure that domains are added only once
28
         */
29
        $this->searchDomains[] = $domain;
30
    }
31
32
    /**
33
     * Search across all search domains in the engine
34
     *
35
     * @param string $query
36
     * @return SearchResult[] Orded by descending relevance score
37
     */
38
    public function search($query)
39
    {
40
        /*
41
         * TODO add search syntax (name:, after:, before:, +/-, etc.)
42
         */
43
        $results = [];
44
        foreach ($this->searchDomains as $domain) {
45
            $results = array_merge($domain->search($query), $results);
46
        }
47
        $this->sortByRelevance($results);
48
        return $results;
49
    }
50
51
    /**
52
     * Generate a displayable list of the included search domains
53
     *
54
     * @param boolean $includeUrl (Optional, defaults to `true`) Should the
55
     *                            URLs of the search domains be included as
56
     *                            well as the names?
57
     * @param boolean $htmlFormatted (Optional, defaults to `true`) Should the
58
     *                               list of search domains be HTML formatted?
59
     * @param boolean $includeIcon (Optional, defaults to `true`) Should the
60
     *                             domains include their icons (valid only if
61
     *                             HTML-formatted)
62
     * @param string $separator (Optional, defaults to `', '`)
63
     * @return string List of search domains aggregated by this engine (for
64
     *                display)
65
     */
66
    public function getDomains($includeUrl = true, $htmlFormatted = true, $includeIcon = true, $separator = ', ')
67
    {
68
        $domains = [];
69
        foreach ($this->searchDomains as $domain) {
70
            if ($includeUrl) {
71
                if ($htmlFormatted) {
72
                    $domains[] = '<a href="' . $domain->getUrl() . '">' .
73
                        ($includeIcon ?
74
                            '<img src="' . $domain->getIcon() . '" class="domain-icon" />': ''
75
                        ) . $domain->getName() .
76
                        '</a>';
77
                } else {
78
                    $domains[] = $domain->getName() . '(' . $domain->getUrl() . ')';
79
                }
80
            } else {
81
                $domains[] = $domain->getName();
82
            }
83
        }
84
        return implode($separator, $domains);
85
    }
86
}
87