Completed
Push — master ( 102796...56544d )
by Adam
9s
created

Insert::addUid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace BestServedCold\LaravelZendSearch\Lucene\Store;
4
5
use BestServedCold\LaravelZendSearch\Lucene\Index;
6
use ZendSearch\Lucene\Document;
7
use ZendSearch\Lucene\Document\Field;
8
9
/**
10
 * Class Insert
11
 *
12
 * @package BestServedCold\LaravelZendSearch\Lucene\Store
13
 */
14
class Insert
15
{
16
    /**
17
     * @var Document
18
     */
19
    private $document;
20
21
    /**
22
     * @var float
23
     */
24
    private $defaultBoost = 1.0;
25
26
    /**
27
     * Insert constructor.
28
     *
29
     * @param Document $document
30
     */
31 22
    public function __construct(Document $document)
32
    {
33 22
        $this->document = $document;
34 22
    }
35
36
    /**
37
     * Insert
38
     *
39
     * @param  integer $id
40
     * @param  Index          $index
41
     * @param  array          $fields      fields that are indexed
42
     * @param  boolean|string $uid         unique identifier, if required
43
     * @param  array          $boostFields
44
     * @return mixed
45
     */
46 1
    public function insert(Index $index, $id, array $fields, $uid = false, array $boostFields = [])
47
    {
48 1
        $this->document->addField($this->field('xref_id', $id));
49 1
        $this->document = $this->addUid($this->document, $uid);
50 1
        $this->document = $this->addFields($this->document, $fields, $boostFields);
51 1
        return $index->get()->addDocument($this->document);
52
    }
53
54
    /**
55
     * @param  string|integer $field
56
     * @param  string         $value
57
     * @param  string         $type
58
     * @return Field
59
     */
60 1
    private function field($field, $value, $type = 'keyword', $boost = null)
61
    {
62 1
        $field = Field::$type($field, strtolower(strip_tags($value)));
63 1
        $field->boost = $boost ?: $this->defaultBoost;
64 1
        return $field;
65
    }
66
67
    /**
68
     * @param  Document       $document
69
     * @param  boolean|string $uid
70
     * @return Document
71
     */
72 1
    private function addUid(Document $document, $uid = false)
73
    {
74 1
        if ($uid) {
75 1
            $document->addField($this->field('uid', $uid));
0 ignored issues
show
Bug introduced by
It seems like $uid defined by parameter $uid on line 72 can also be of type boolean; however, BestServedCold\LaravelZe...e\Store\Insert::field() does only seem to accept string, 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...
76 1
        }
77
78 1
        return $document;
79
    }
80
81
    /**
82
     * @param  Document $document
83
     * @param  array    $fields
84
     * @param  array    $boostFields
85
     * @return Document
86
     */
87 1
    private function addFields(Document $document, array $fields, array $boostFields = [])
88
    {
89 1
        foreach ($fields as $field => $text) {
90 1
            $document->addField($this->field($field, $text, 'text', $this->boost($field, $boostFields)));
91 1
        }
92
93 1
        return $document;
94
    }
95
96
    /**
97
     * @param  $field
98
     * @param  array      $boostFields
99
     * @return mixed|null
100
     */
101 2
    private function boost($field, array $boostFields = [])
102
    {
103 2
        if (empty($boostFields)) {
104 1
            return null;
105
        }
106 1
        return array_key_exists($field, $boostFields) ? $boostFields[$field] : null;
107
    }
108
}
109