AbstractBulkUpdateRequest::setBody()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
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
abstract class AbstractBulkUpdateRequest extends AbstractBaseRequest
31
{
32
    const LINE_BREAK = "\n";
33
    const REQUEST_ACTION = '_bulk';
34
    const BULK_ACTION = 'update';
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 9
    public function getBody()
83
    {
84 9
        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
     * @param null|int $retryOnConflict
101
     *
102
     * @author Daniel Wendlandt
103
     */
104 6
    public function addDocument($doc, $id, $retryOnConflict = null)
105
    {
106
        $action = array(
107 6
            self::BULK_ACTION => array(
108 6
                '_id' => $id,
109 6
                '_index' => $this->index,
110 6
                '_type' => $this->type
111 6
            )
112 6
        );
113
114 6
        if(null !== $retryOnConflict) {
115 3
            $action[self::BULK_ACTION]['_retry_on_conflict'] = (int) $retryOnConflict;
116 3
        }
117
118 6
        $this->body .= json_encode($action) . self::LINE_BREAK;
119 6
        $this->body .= '{"doc":' . $this->serializer->serialize($doc, $this->serializerParams) . '}' . self::LINE_BREAK;
120 6
    }
121
}
122