Completed
Push — master ( 56544d...e42d15 )
by Adam
10s
created

Index   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 108
ccs 28
cts 28
cp 1
rs 10
c 2
b 0
f 0

7 Methods

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