Completed
Push — cat-api ( f87ad4 )
by Daniel
03:23
created

AbstractAllocationCatResponse::getProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4286
cc 1
eloc 10
nc 1
nop 0
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
namespace Elastification\Client\Response\Shared\Cat;
19
20
use Elastification\Client\Response\Response;
21
22
/**
23
 * Class AbstractAliasesCatResponse
24
 *
25
 * @package Elastification\Client\Response\Shared\Cat
26
 * @author  Daniel Wendlandt
27
 */
28
abstract class AbstractAllocationCatResponse extends Response
29
{
30
    const PROP_SHARDS = 'shards';
31
    const PROP_DISK_USED = 'disk.used';
32
    const PROP_DISK_AVAIL = 'disk.avail';
33
    const PROP_DISK_TOTAL = 'disk.total';
34
    const PROP_DISK_PERCENT = 'disk.percent';
35
    const PROP_HOST = 'host';
36
    const PROP_IP = 'id';
37
    const PROP_NODE = 'node';
38
39
    /**
40
     * @inheritdoc
41
     */
42 2 View Code Duplication
    protected function processData()
0 ignored issues
show
Duplication introduced by
This method 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...
43
    {
44 2
        if (null === $this->data) {
45 2
            $rawData = $this->getRawData();
46
47 2
            if (null !== $rawData) {
48 2
                $this->data = $this->processCatData($rawData);
49 2
            }
50 2
        }
51 2
    }
52
53
    /**
54
     * This is our serializer ;)
55
     *
56
     * @param string $rawData
57
     *
58
     * @return array
59
     */
60 2
    protected function processCatData($rawData)
61
    {
62 2
        $data = array();
63 2
        $properties = $this->getProperties();
64 2
        $rows = explode(PHP_EOL, $rawData);
65
66 2
        foreach ($rows as $row) {
67 2
            if (mb_strlen($row) > 3) {
68 2
                $fields = array_filter(explode(' ', $row), function($value) {
69 2
                    return mb_strlen($value) !== 0;
70 2
                });
71
72 2
                $cntProps = count($properties);
73 2
                $cntFields = count($fields);
74
75 2
                if ($cntProps != $cntFields) {
76 2
                    $diff = $cntFields - $cntProps + 1;
77 2
                    $node = [];
78
79 2
                    for($i = 1; $i <= $diff; $i++) {
80 2
                        $node[] = array_pop($fields);
81 2
                    }
82
83 2
                    $fields[] = implode(' ', array_reverse($node));
84 2
                }
85
86 2
                $data[] = array_combine($properties, $fields);
87 2
            }
88 2
        }
89
90 2
        return $data;
91
    }
92
93
    /**
94
     * Provides properties of this cat request
95
     *
96
     * @return array
97
     */
98 2
    protected function getProperties()
99
    {
100
        return array(
101 2
            self::PROP_SHARDS,
102 2
            self::PROP_DISK_USED,
103 2
            self::PROP_DISK_AVAIL,
104 2
            self::PROP_DISK_TOTAL,
105 2
            self::PROP_DISK_PERCENT,
106 2
            self::PROP_HOST,
107 2
            self::PROP_IP,
108
            self::PROP_NODE
109 2
        );
110
    }
111
}
112