Completed
Branch master (b4faf9)
by Adam
05:11
created

Index::createIndex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
crap 3
1
<?php
2
3
namespace BestServedCold\LaravelZendSearch\Lucene;
4
5
use ZendSearch\Lucene\Analysis\Analyzer\Analyzer;
6
use ZendSearch\Lucene\Analysis\Analyzer\Common\Utf8Num\CaseInsensitive;
7
use ZendSearch\Lucene\Lucene;
8
use ZendSearch\Exception\ExceptionInterface;
9
use ZendSearch\Lucene\Index as LuceneIndex;
10
use ZendSearch\Lucene\SearchIndexInterface;
11
12
/**
13
 * Class Index
14
 * @package BestServedCold\LaravelZendSearch\Lucene
15
 */
16
class Index
17
{
18
    /**
19
     * @var LuceneIndex
20
     */
21
    private $index;
22
23
    /**
24
     * @var string|boolean $path
25
     */
26
    protected $path;
27
28
    /**
29
     * Open
30
     *
31
     * @param  bool $path
32
     * @param  bool $forceCreate
33
     * @return $this
34
     * @throws ExceptionInterface
35
     * @throws \Exception
36
     */
37 9
    public function open($path = false, $forceCreate = true)
38
    {
39 9
        $this->path = $path ? $path : $this->path;
40 9
        Analyzer::setDefault(new CaseInsensitive);
41
42 9
        $this->index = $this->createIndex($this->path(), $forceCreate);
43 7
        return $this;
44
    }
45
46
    /**
47
     * @param integer $limit
48
     * @return $this
49
     */
50 1
    public function limit($limit)
51
    {
52 1
        Lucene::setResultSetLimit($limit);
53 1
        return $this;
54
    }
55
56
    /**
57
     * Create Index
58
     *
59
     * Extends the Lucene "open" method to create the index if it doesn't exist.
60
     *
61
     * @param  string|boolean $path
62
     * @param  boolean        $forceCreate
63
     * @return SearchIndexInterface
64
     * @throws \Exception
65
     */
66 8
    private function createIndex($path, $forceCreate = true)
67 1
    {
68
        try {
69 8
            $index = Lucene::open($path);
70 8
        } catch (ExceptionInterface $error) {
71 8
            if ($forceCreate) {
72 7
                $index = Lucene::create($path);
73 7
            } else {
74 1
                throw $error;
75
            }
76
        }
77
        
78 7
        return $index;
79
    }
80
81
    /**
82
     * @param $path
83
     */
84 20
    public function setPath($path)
85
    {
86 20
        $this->path = $path;
87 20
    }
88
89
    /**
90
     * @return string
91
     * @throws \Exception
92
     */
93 9
    protected function path()
94
    {
95 9
        if (!$this->path) {
96 1
            throw new \Exception('No path specified nor config variable set.');
97
        }
98
99 8
        return $this->path;
100
    }
101
102
    /**
103
     * @return LuceneIndex
104
     */
105 5
    public function get()
106
    {
107 5
        return $this->index;
108
    }
109
}
110