Completed
Push — master ( f8c9ff...80eb7e )
by Adam
03:35
created

Insert::addParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 6
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  array          $parameters fields that aren't indexed
47
     * @param  boolean|string $uid        unique identifier, if required
48
     * @return mixed
49
     */
50
    public function insert($id, array $fields, array $parameters = [ ], $uid = false)
51
    {
52
        $this->document->addField($this->field('xref_id', $id));
53
        $this->document = $this->addUid($this->document, $uid);
0 ignored issues
show
Bug introduced by
It seems like $uid defined by parameter $uid on line 50 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...
54
        $this->document = $this->addFields($this->document, $fields);
55
        $this->document = $this->addParameters($this->document, $parameters);
56
        return $this->index->get()->addDocument($this->document);
57
    }
58
59
    /**
60
     * @param Document $document
61
     * @param array $parameters
62
     * @return Document
63
     */
64
    private function addParameters(Document $document, array $parameters = [])
65
    {
66
        if (! empty($parameters)) {
67
            $document->addField($this->field('_parameters', $this->flattenParameters($parameters), 'unIndexed'));
68
        }
69
70
        return $document;
71
    }
72
73
    /**
74
     * @param  string $keyword
75
     * @param  string $value
76
     * @param  string $type
77
     * @return Field
78
     */
79
    private function field($keyword, $value, $type = 'keyword')
80
    {
81
        return Field::$type($keyword, $value);
82
    }
83
84
    /**
85
     * @param  Document $document
86
     * @param  bool     $uid
87
     * @return Document
88
     */
89
    private function addUid(Document $document, $uid = false)
90
    {
91
        if ($uid) {
92
            $document->addField($this->field('uid', strtoupper($uid)));
93
        }
94
95
        return $document;
96
    }
97
98
    /**
99
     * @param  Document $document
100
     * @param  array    $fields
101
     * @return Document
102
     */
103
    private function addFields(Document $document, array $fields)
104
    {
105
        foreach ($fields as $key => $field) {
106
            $document->addField($this->field($key, strtoupper($field)));
107
        }
108
109
        return $document;
110
    }
111
112
    /**
113
     * @param  $parameters
114
     * @return string
115
     * @todo   I'm not sure this is the right thing to do here, perhaps do some more investigation.
116
     */
117
    private function flattenParameters($parameters)
118
    {
119
        return base64_encode(json_encode($parameters));
120
    }
121
}
122