Completed
Branch new-instance (cdb4e5)
by Hector
02:51
created

Cursor::fetchNext()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
rs 8.5906
cc 5
eloc 19
nc 5
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: hborras
5
 * Date: 2/04/16
6
 * Time: 23:14.
7
 */
8
namespace Hborras\TwitterAdsSDK\TwitterAds;
9
10
use Hborras\TwitterAdsSDK\TwitterAds;
11
12
class Cursor implements \IteratorAggregate
13
{
14
    /** @var Resource  */
15
    private $resource;
16
    private $params;
17
    private $next_cursor = null;
18
    private $current_index = 0;
19
    private $total_count = 0;
20
    /** @var  array */
21
    private $collection;
22
    /** @var  TwitterAds */
23
    private $twitterAds;
24
25
    public function __construct(/* @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
26
        \Hborras\TwitterAdsSDK\TwitterAds\Resource $resource, TwitterAds $twitterAds, $request, $params)
27
    {
28
        $this->resource = $resource;
0 ignored issues
show
Documentation Bug introduced by
It seems like $resource of type object<Hborras\TwitterAdsSDK\TwitterAds\Resource> is incompatible with the declared type resource of property $resource.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
        $this->params = $params;
30
        $this->twitterAds = $twitterAds;
31
        $this->fromResponse($request);
32
    }
33
34
    /**
35
     * @return bool
36
     */
37
    public function isExhausted()
38
    {
39
        return is_null($this->next_cursor) ? true : false;
40
    }
41
42
    /**
43
     * @return integer
44
     */
45
    public function count()
46
    {
47
        return max($this->total_count, count($this->collection));
48
    }
49
50
    /**
51
     * @return Resource
52
     */
53
    public function first()
54
    {
55
        return $this->next();
56
    }
57
58
    /**
59
     * @return int
60
     */
61
    public function fetched()
62
    {
63
        return count($this->collection);
64
    }
65
66
    /**
67
     * @return Resource | 0
68
     */
69
    public function next()
70
    {
71
        if ($this->current_index < count($this->collection)) {
72
            $value = $this->collection[$this->current_index];
73
            ++$this->current_index;
74
75
            return $value;
76
        } elseif (!is_null($this->next_cursor)) {
77
            $this->fetchNext();
78
79
            return $this->next();
80
        } else {
81
            $this->current_index = 0;
82
83
            return 0;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 0; (integer) is incompatible with the return type documented by Hborras\TwitterAdsSDK\TwitterAds\Cursor::next of type resource.

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...
84
        }
85
    }
86
87
    /**
88
     * @param array $params
89
     * @return Cursor
90
     */
91
    public function fetchNext($params = [])
92
    {
93
        $params['cursor'] = $this->next_cursor;
94
        switch ($this->getTwitterAds()->getMethod()) {
95
            case 'GET':
96
                $response = $this->getTwitterAds()->get($this->getTwitterAds()->getResource(), $params);
97
            break;
98
            case 'POST':
99
                $response = $this->getTwitterAds()->post($this->getTwitterAds()->getResource(), $params);
100
            break;
101
            case 'PUT':
102
                $response = $this->getTwitterAds()->put($this->getTwitterAds()->getResource(), $params);
103
            break;
104
            case 'DELETE':
105
                $response = $this->getTwitterAds()->delete($this->getTwitterAds()->getResource());
106
            break;
107
            default:
108
                $response = $this->getTwitterAds()->get($this->getTwitterAds()->getResource(), $params);
109
            break;
110
        }
111
112
        return $this->fromResponse($response->getBody());
113
    }
114
115
    /**
116
     * @param $request
117
     * @return $this
118
     */
119
    public function fromResponse($request)
120
    {
121
        $this->next_cursor = isset($request->next_cursor) ? $request->next_cursor : null;
122
        if (isset($request->total_count)) {
123
            $this->total_count = intval($request->total_count);
124
        }
125
        foreach ($request->data as $item) {
126
            if (method_exists($this->resource, 'fromResponse')) {
127
                $obj = new $this->resource();
128
                $this->collection[] = $obj->fromResponse($item);
129
            } else {
130
                $this->collection[] = $item;
131
            }
132
        }
133
134
        return $this;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function getIterator()
141
    {
142
        return new \ArrayIterator($this->collection);
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    public function getCollection()
149
    {
150
        return $this->collection;
151
    }
152
153
    /**
154
     * @param array $collection
155
     */
156
    public function setCollection($collection)
157
    {
158
        $this->collection = $collection;
159
    }
160
161
    /**
162
     * @return TwitterAds
163
     */
164
    public function getTwitterAds()
165
    {
166
        return $this->twitterAds;
167
    }
168
169
    /**
170
     * @param TwitterAds $twitterAds
171
     */
172
    public function setTwitterAds($twitterAds)
173
    {
174
        $this->twitterAds = $twitterAds;
175
    }
176
}
177