Completed
Push — master ( 261f00...a2b53b )
by Adam
02:35
created

Insert::boost()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 4
nc 3
nop 2
crap 3
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
     * @var null|Document
28
     */
29
    private static $lastInsert = null;
30
31
    /**
32
     * Insert constructor.
33
     *
34
     * @param Document $document
35
     */
36 23
    public function __construct(Document $document)
37
    {
38 23
        $this->document = $document;
39 23
    }
40
41
    /**
42
     * Insert
43
     *
44
     * @param  integer $id
45
     * @param  Index          $index
46
     * @param  array          $fields      fields that are indexed
47
     * @param  boolean|string $uid         unique identifier, if required
48
     * @param  array          $boostFields
49
     * @return mixed
50
     */
51 1
    public function insert(Index $index, $id, array $fields, $uid = false, array $boostFields = [])
52
    {
53 1
        $this->document->addField($this->field('xref_id', $id));
54 1
        $this->document = $this->addUid($this->document, $uid);
55 1
        $this->document = $this->addFields($this->document, $fields, $boostFields);
56 1
        self::$lastInsert = $this->document;
57 1
        return $index->get()->addDocument($this->document);
58
    }
59
60
    /**
61
     * @return null|Document
62
     */
63 1
    public static function getLastInsert()
64
    {
65 1
        return self::$lastInsert;
66
    }
67
68
    /**
69
     * @param  string|integer $field
70
     * @param  string         $value
71
     * @param  string         $type
72
     * @return Field
73
     */
74 1
    private function field($field, $value, $type = 'keyword', $boost = null)
75
    {
76 1
        $field = Field::$type($field, strtolower(strip_tags($value)));
77 1
        $field->boost = $boost ?: $this->defaultBoost;
78 1
        return $field;
79
    }
80
81
    /**
82
     * @param  Document       $document
83
     * @param  boolean|string $uid
84
     * @return Document
85
     */
86 1
    private function addUid(Document $document, $uid = false)
87
    {
88 1
        $uid && is_string($uid) ? $document->addField($this->field('uid', $uid)) : null;
89 1
        return $document;
90
    }
91
92
    /**
93
     * @param  Document $document
94
     * @param  array    $fields
95
     * @param  array    $boostFields
96
     * @return Document
97
     */
98 1
    private function addFields(Document $document, array $fields, array $boostFields = [])
99
    {
100 1
        foreach ($fields as $field => $text) {
101 1
            $document->addField($this->field($field, $text, 'text', $this->boost($field, $boostFields)));
102 1
        }
103
104 1
        return $document;
105
    }
106
107
    /**
108
     * @param  $field
109
     * @param  array      $boostFields
110
     * @return mixed|null
111
     */
112 2
    private function boost($field, array $boostFields = [])
113
    {
114 2
        if (empty($boostFields)) {
115 1
            return null;
116
        }
117 1
        return array_key_exists($field, $boostFields) ? $boostFields[$field] : null;
118
    }
119
}
120