Completed
Push — master ( b46976...b7332f )
by Adam
13s
created

Index   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90.63%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 117
ccs 29
cts 32
cp 0.9063
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A open() 0 7 2
A close() 0 5 1
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
     * @return $this
60
     */
61
    public function close()
62
    {
63
        $this->index = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object<ZendSearch\Lucene\Index> of property $index.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
        return $this;
65
    }
66
67
    /**
68
     * @param integer $limit
69
     * @return $this
70
     */
71 1
    public function limit($limit)
72
    {
73 1
        Lucene::setResultSetLimit($limit);
74 1
        return $this;
75 1
    }
76
77
    /**
78
     * Create Index
79
     *
80
     * Extends the Lucene "open" method to create the index if it doesn't exist.
81
     *
82
     * @param  string|boolean $path
83
     * @param  boolean        $forceCreate
84
     * @return SearchIndexInterface
85
     * @throws \Exception
86
     */
87 8
    private function createIndex($path, $forceCreate = true)
88
    {
89
        try {
90 8
            $index = Lucene::open($path);
91 8
        } catch (ExceptionInterface $error) {
92 8
            if ($forceCreate) {
93 7
                $index = Lucene::create($path);
94 7
            } else {
95 1
                throw $error;
96
            }
97
        }
98
        
99 7
        return $index;
100
    }
101
102
    /**
103
     * @param $path
104
     */
105 23
    public function setPath($path)
106
    {
107 23
        $this->path = $path;
108 23
    }
109
110
    /**
111
     * @return string
112
     * @throws \Exception
113
     */
114 9
    protected function path()
115
    {
116 9
        if (!$this->path) {
117 1
            throw new \Exception('No path specified nor config variable set.');
118
        }
119
120 8
        return $this->path;
121
    }
122
123
    /**
124
     * @return LuceneIndex
125
     */
126 5
    public function get()
127
    {
128 5
        return $this->index;
129
    }
130
}
131