Completed
Push — master ( 8b06e6...a27575 )
by Adam
03:27
created

Index::path()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2.0625
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 static $index;
22
23
    /**
24
     * @var string
25
     */
26
    protected $path;
27
28
    /**
29
     * Open
30
     *
31
     * @param  bool $path
32
     * @param  bool $forceCreate
33
     * @return \ZendSearch\Lucene\SearchIndexInterface
34
     * @throws ExceptionInterface
35
     * @throws \Exception
36
     */
37 4
    public function open($path = false, $forceCreate = true)
38
    {
39 4
        $path ? $this->path = $path : null;
0 ignored issues
show
Documentation Bug introduced by
The property $path was declared of type string, but $path is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
40 4
        Analyzer::setDefault(new CaseInsensitive);
41 4
        self::$index = $this->index($this->path(), $forceCreate);
42 3
        return self::$index;
43
    }
44
45
    /**
46
     * @param integer $limit
47
     * @return $this
48
     */
49 1
    public function limit($limit)
50
    {
51 1
        Lucene::setResultSetLimit($limit);
52 1
        return $this;
53
    }
54
55
    /**
56
     * Index
57
     *
58
     * Extends the Lucene "open" method to create the index if it doesn't exist.
59
     *
60
     * @param  string|boolean $path
61
     * @param  boolean        $forceCreate
62
     * @return SearchIndexInterface
63
     * @throws \Exception
64
     */
65 4
    private function index($path, $forceCreate = true)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
66
    {
67 1
        try {
68 4
            $index = Lucene::open($path);
69 4
        } catch (ExceptionInterface $error) {
70 4
            if ($forceCreate) {
71 3
                $index = Lucene::create($path);
72 3
            } else {
73 1
                throw $error;
74
            }
75
        }
76
        
77 3
        return $index;
78
    }
79
80
    /**
81
     * @param $path
82
     */
83 1
    public function setPath($path)
84
    {
85 1
        $this->path = $path;
86 1
    }
87
88
    /**
89
     * @return string
90
     * @throws \Exception
91
     */
92 4
    protected function path()
93
    {
94 4
        if (!$this->path) {
95
            throw new \Exception('No path specified nor config variable set.');
96
        }
97
98 4
        return $this->path;
99
    }
100
101
    /**
102
     * @return LuceneIndex
103
     */
104 1
    public function get()
105
    {
106 1
        return self::$index;
107
    }
108
}
109