Passed
Branch master (3a11a5)
by Adam
06:46 queued 03:28
created

Insert::field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 3
crap 2
1
<?php
2
3
namespace BestServedCold\LaravelZendSearch\Lucene\Store;
4
5
use BestServedCold\LaravelZendSearch\Lucene\Index;
6
use BestServedCold\LaravelZendSearch\Lucene\Search;
7
use ZendSearch\Lucene\Document;
8
use ZendSearch\Lucene\Document\Field;
9
10
/**
11
 * Class Insert
12
 *
13
 * @package BestServedCold\LaravelZendSearch\Lucene\Store
14
 */
15
final class Insert
16
{
17
    /**
18
     * @var Index
19
     */
20
    private $index;
21
22
    /**
23
     * @var Document
24
     */
25
    private $document;
26
27
    /**
28
     * Insert constructor.
29
     *
30
     * @param Search   $search
31
     * @param Index    $index
32
     * @param Document $document
33
     */
34
    public function __construct(Search $search, Index $index, Document $document)
35
    {
36
        $this->search   = $search;
0 ignored issues
show
Bug introduced by
The property search does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
        $this->index    = $index;
38
        $this->document = $document;
39
    }
40
41
    /**             
42
     * Insert
43
     *
44
     * @param  $id
45
     * @param  array          $fields     fields that are indexed
46
     * @param  boolean|string $uid        unique identifier, if required
47
     * @return mixed
48
     */
49
    public function insert($id, array $fields, $uid = false)
50
    {
51
        $this->document->addField($this->field('xref_id', $id));
52
        $this->document = $this->addUid($this->document, $uid);
0 ignored issues
show
Bug introduced by
It seems like $uid defined by parameter $uid on line 49 can also be of type string; however, BestServedCold\LaravelZe...\Store\Insert::addUid() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
53
        $this->document = $this->addFields($this->document, $fields);
54
        return $this->index->get()->addDocument($this->document);
55
    }
56
57
    /**
58
     * @param  string $keyword
59
     * @param  string $value
60
     * @param  string $type
61
     * @return Field
62
     */
63
    private function field($keyword, $value, $type = 'keyword')
64
    {
65
        return Field::$type($keyword, $value);
66
    }
67
68
    /**
69
     * @param  Document $document
70
     * @param  bool     $uid
71
     * @return Document
72
     */
73
    private function addUid(Document $document, $uid = false)
74
    {
75
        if ($uid) {
76
            $document->addField($this->field('uid', strtoupper($uid)));
77
        }
78
79
        return $document;
80
    }
81
82
    /**
83
     * @param  Document $document
84
     * @param  array    $fields
85
     * @return Document
86
     */
87
    private function addFields(Document $document, array $fields)
88
    {
89
        foreach ($fields as $key => $field) {
90
            $document->addField($this->field($key, strtoupper($field)));
91
        }
92
93
        return $document;
94
    }
95
}
96