AbstractBulkIndexRequest::setBody()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 4
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
namespace Elastification\Client\Request\Shared\Bulk;
20
21
use Elastification\Client\Request\RequestMethods;
22
use Elastification\Client\Request\Shared\AbstractBaseRequest;
23
24
/**
25
 * Class AbstractCreateIndexRequest
26
 *
27
 * @package Elastification\Client\Request\Shared\Index
28
 * @author  Daniel Wendlandt
29
 */
30 View Code Duplication
abstract class AbstractBulkIndexRequest extends AbstractBaseRequest
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
{
32
    const LINE_BREAK = "\n";
33
    const REQUEST_ACTION = '_bulk';
34
    const BULK_ACTION = 'index';
35
36
    /**
37
     * @var null|mixed
38
     */
39
    private $body = null;
40
41
    /**
42
     * This will overwrite getIndex method for returning null.
43
     * The index property is only for internal use.
44
     *
45
     * @return null
46
     */
47 3
    public function getIndex()
48
    {
49 3
        return null;
50
    }
51
52
    /**
53
     * This will overwrite getType method for returning null.
54
     * The type property is only for internal use.
55
     *
56
     * @return null
57
     */
58 3
    public function getType()
59
    {
60 3
        return null;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 3
    public function getMethod()
67
    {
68 3
        return RequestMethods::POST;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 3
    public function getAction()
75
    {
76 3
        return self::REQUEST_ACTION;
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82 6
    public function getBody()
83
    {
84 6
        return $this->body;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90 3
    public function setBody($body)
91
    {
92
        //to nothing
93 3
    }
94
95
    /**
96
     * adds a document to the body and transforms it in the right format.
97
     *
98
     * @param array|object $doc
99
     * @param string $id
100
     * @author Daniel Wendlandt
101
     */
102 3
    public function addDocument($doc, $id = null)
103
    {
104
        $action = array(
105 3
            self::BULK_ACTION => array(
106 3
                '_id' => $id,
107 3
                '_index' => $this->index,
108 3
                '_type' => $this->type
109 3
            )
110 3
        );
111
112 3
        $this->body .= json_encode($action) . self::LINE_BREAK;
113 3
        $this->body .= $this->serializer->serialize($doc, $this->serializerParams) . self::LINE_BREAK;
114 3
    }
115
}
116