Completed
Pull Request — master (#14)
by
unknown
11:22
created

Entry::__construct()   C

Complexity

Conditions 11
Paths 9

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 11

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 17
c 2
b 0
f 0
nc 9
nop 1
dl 0
loc 28
ccs 18
cts 18
cp 1
crap 11
rs 5.2653

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Directus – <http://getdirectus.com>
5
 *
6
 * @link      The canonical repository – <https://github.com/directus/directus>
7
 * @copyright Copyright 2006-2016 RANGER Studio, LLC – <http://rangerstudio.com>
8
 * @license   GNU General Public License (v3) – <http://www.gnu.org/copyleft/gpl.html>
9
 */
10
11
namespace Directus\SDK\Response;
12
use Directus\Util\ArrayUtils;
13
14
/**
15
 * Entry
16
 *
17
 * @author Welling Guzmán <[email protected]>
18
 */
19
class Entry implements ResponseInterface, \ArrayAccess
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $data = null;
25
26
    /**
27
     * @var array
28
     */
29
    protected $rawData = null;
30
31
    /**
32
     * @var Entry
33
     */
34
    protected $metadata = null;
35
36
    /**
37
     * Entry constructor.
38
     *
39
     * @param $data
40
     */
41 58
    public function __construct($data)
42
    {
43 58
        $this->rawData = $data;
44 58
        if (!is_array($data)) {
45 10
            return;
46
        }
47
48
        // Support API 1.1
49 58
        if (isset($data['data']) && is_array($data['data'])) {
50 2
            $this->metadata = new static(ArrayUtils::get($data, 'meta', []));
51 2
            unset($data['meta']);
52
53 2
            $data = $data['data'];
54 2
        }
55
56 58
        foreach ($data as $field => $value) {
57 58
            if ((isset($value['rows']) &&
58 14
                    (array_key_exists('id', $value) && !in_array($value['id'], ['textarea', 'textinput'])))
59 58
                || (isset($value['data']) && ArrayUtils::isNumericKeys($value['data']))
60 12
            ) {
61 12
                $this->data[$field] = new EntryCollection($value);
62 58
            } else if (is_array($value)) {
63
                $this->data[$field] = new static($value);
64 58
            } else {
65 58
                $this->data[$field] = $value;
66
            }
67
        }
68
    }
69
70
    /**
71
     * Get the entry data
72 8
     *
73
     * @return array
74 8
     */
75
    public function getData()
76
    {
77
        return $this->data;
78
    }
79
80
    /**
81
     * Get the entry metadata
82 8
     *
83
     * @return Entry
84 8
     */
85
    public function getMetaData()
86
    {
87
        return $this->metadata;
88
    }
89
90 16
    /**
91
     * @return array
92 16
     */
93
    public function getRawData()
94
    {
95 2
        return $this->rawData;
96
    }
97 2
98
    public function offsetExists($offset)
99
    {
100 4
        return array_key_exists($offset, $this->data);
101
    }
102 4
103
    public function offsetGet($offset)
104
    {
105 2
        return $this->data[$offset];
106
    }
107 2
108
    public function offsetSet($offset, $value)
109
    {
110 2
        throw new \BadMethodCallException('Entry is read only');
111
    }
112 2
113
    public function offsetUnset($offset)
114
    {
115 4
        throw new \BadMethodCallException('Entry is read only');
116
    }
117 4
118 2
    public function __get($name)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
119
    {
120
        if (array_key_exists($name, $this->data)) {
121 2
            return $this->data[$name];
122
        }
123
124 2
        throw new \InvalidArgumentException('Invalid property: ' . $name);
125
    }
126 2
127
    public function __set($name, $value)
128
    {
129
        throw new \BadMethodCallException('Entry is read only');
130
    }
131
132
    /**
133
     * Gets the object representation of this entry
134 4
     *
135
     * @return object
136
     */
137 4
    public function jsonSerialize()
138 4
    {
139 4
        return (object) [
140
            'metadata' => $this->getMetaData(),
141
            'data' => $this->getData()
142
        ];
143
    }
144
}