Completed
Push — master ( 34e9c0...86221b )
by Welling
03:45
created

EntryCollection::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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
13
use Directus\Util\ArrayUtils;
14
15
/**
16
 * Entry Collection
17
 *
18
 * @author Welling Guzmán <[email protected]>
19
 */
20
class EntryCollection implements ResponseInterface, \IteratorAggregate, \ArrayAccess, \Countable
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $items = [];
26
27
    /**
28
     * @var array
29
     */
30
    protected $rawData = [];
31
32
    /**
33
     * @var Entry
34
     */
35
    protected $metadata = [];
36
37
    /**
38
     * EntryCollection constructor.
39
     *
40
     * @param $data
41
     */
42 20
    public function __construct($data)
43
    {
44 20
        $this->rawData = $data;
45 20
        $this->metadata = $this->pickMetadata($data);
46
47 20
        $rows = $this->pickRows($data);
48 20
        $items = [];
49 20
        foreach($rows as $row) {
50 20
            $items[] = new Entry($row);
51 20
        }
52
53 20
        $this->items = $items;
54 20
    }
55
56
    /**
57
     * Pick the metadata out of the raw data
58
     *
59
     * @param $data
60
     *
61
     * @return Entry
62
     */
63 20
    protected function pickMetadata($data)
64
    {
65 20
        $metadata = [];
66 20 View Code Duplication
        if (ArrayUtils::has($data, 'rows')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
67 20
            $metadata = ArrayUtils::omit($data, 'rows');
68 20
        } else if (ArrayUtils::has($data, 'meta')) {
69 14
            $metadata = ArrayUtils::get($data, 'meta');
70 14
        }
71
72 20
        return new Entry($metadata);
73
    }
74
75
    /**
76
     * Pick the "rows" (items) out of the raw data
77
     *
78
     * @param $data
79
     *
80
     * @return array
81
     */
82 20
    protected function pickRows($data)
83
    {
84 20
        $rows = [];
85 20 View Code Duplication
        if (ArrayUtils::has($data, 'rows')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
86 20
            $rows = ArrayUtils::get($data, 'rows', []);
87 20
        } else if (ArrayUtils::has($data, 'data')) {
88 14
            $rows = ArrayUtils::get($data, 'data', []);
89 14
        }
90
91 20
        return $rows;
92
    }
93
94
    /**
95
     * Get the response raw data
96
     *
97
     * @return array
98
     */
99 2
    public function getRawData()
100
    {
101 2
        return $this->rawData;
102
    }
103
104
    /**
105
     * Get the response entries
106
     *
107
     * @return array
108
     */
109 2
    public function getData()
110
    {
111 2
        return $this->items;
112
    }
113
114
    /**
115
     * Get the response metadata
116
     *
117
     * @return Entry
118
     */
119 4
    public function getMetaData()
120
    {
121 4
        return $this->metadata;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->metadata; (Directus\SDK\Response\Entry) is incompatible with the return type declared by the interface Directus\SDK\Response\Re...eInterface::getMetaData of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
122
    }
123
124
    /**
125
     * Create a new iterator based on this collection
126
     *
127
     * @return \ArrayIterator
128
     */
129 2
    public function getIterator()
130
    {
131 2
        return new \ArrayIterator($this->items);
132
    }
133
134 2
    public function offsetExists($offset)
135
    {
136 2
        return array_key_exists($offset, $this->items);
137
    }
138
139 2
    public function offsetGet($offset)
140
    {
141 2
        return $this->items[$offset];
142
    }
143
144 2
    public function offsetSet($offset, $value)
145
    {
146 2
        throw new \BadMethodCallException('EntryCollection is read only');
147
    }
148
149 2
    public function offsetUnset($offset)
150
    {
151 2
        throw new \BadMethodCallException('EntryCollection is read only');
152
    }
153
154
    /**
155
     * Gets the number of entries in this collection
156
     *
157
     * @return int
158
     */
159 4
    public function count()
160
    {
161 4
        return count($this->items);
162
    }
163
164
    /**
165
     * Gets an object representation of this collection
166
     *
167
     * @return object
168
     */
169 2
    public function jsonSerialize()
170
    {
171
        return (object) [
172 2
            'meta' => $this->metadata,
173 2
            'data' => $this->items
174 2
        ];
175
    }
176
}