Completed
Pull Request — master (#12)
by Adam
05:00 queued 01:43
created

Search::findId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BestServedCold\LaravelZendSearch\Laravel;
4
5
use BestServedCold\LaravelZendSearch\Lucene\Query;
6
use BestServedCold\LaravelZendSearch\Lucene\Search as LuceneSearch;
7
8
/**
9
 * Class Search
10
 * @package BestServedCold\LaravelZendSearch\Laravel
11
 */
12
class Search extends LuceneSearch
13
{
14
    use EloquentTrait;
15
16
    private $hits = [];
17
18
    /**
19
     * Search constructor.
20
     * @param Index $index
21
     * @param Query $query
22
     */
23 4
    public function __construct(Index $index, Query $query)
24
    {
25 4
        parent::__construct($index, $query);
26 4
        $this->path(config('search.index.path'));
27 4
    }
28
29
    /**
30
     * @return mixed
31
     */
32
    public function get()
33
    {
34
        return $this->model->whereIn(
35
            $this->key,
36
            $this->hits()
37
        )->get();
38
    }
39
40
    /**
41
     * @param $id
42
     * @return $this
43
     */
44
    public function findId($id)
45
    {
46
        $this->match($id, 'xref_id');
47
        return $this;
48
    }
49
50
    /**
51
     * Hits
52
     *
53
     * We store the hits, no point in running the query twice.  This also allows us to set the UID once for the model
54
     * table name.  A new search instance is required to run against another model.
55
     *
56
     * @return mixed
57
     */
58
    public function hits()
59
    {
60
        if (!empty($this->hits)) {
61
            return $this->hits;
62
        }
63
64
        $this->match($this->uid, 'uid');
65
66
        $this->hits = parent::hits();
67
        return $this->hits;
68
    }
69
70
    /**
71
     * @param $string
72
     * @return $this
73
     */
74
    public function find($string)
75
    {
76
        $this->where($string);
77
        return $this;
78
    }
79
}
80