Completed
Pull Request — master (#3)
by
unknown
03:46
created

Cursor::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
class Cursor implements \IteratorAggregate
11
{
12
    private $account;
13
    /** @var Resource  */
14
    private $resource;
15
    private $params;
16
    private $next_cursor = null;
17
    private $current_index = 0;
18
    private $total_count = 0;
19
    /** @var  array */
20
    private $collection;
21
22
    public function __construct(/* @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
23
        \Hborras\TwitterAdsSDK\TwitterAds\Resource $resource, Account $account, $request, $params)
24
    {
25
        $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...
26
        $this->account = $account;
27
        $this->params = $params;
28
        $this->fromResponse($request);
29
    }
30
31
    /**
32
     * @return bool
33
     */
34
    public function isExhausted()
35
    {
36
        return is_null($this->next_cursor) ? true : false;
37
    }
38
39
    /**
40
     * @return integer
41
     */
42
    public function count()
43
    {
44
        return max($this->total_count, count($this->collection));
45
    }
46
47
    /**
48
     * @return Resource
49
     */
50
    public function first()
51
    {
52
        return $this->next();
53
    }
54
55
    /**
56
     * @return int
57
     */
58
    public function fetched()
59
    {
60
        return count($this->collection);
61
    }
62
63
    /**
64
     * @return Resource | 0
65
     */
66
    public function next()
67
    {
68
        if ($this->current_index < count($this->collection)) {
69
            $value = $this->collection[$this->current_index];
70
            ++$this->current_index;
71
72
            return $value;
73
        } elseif (!is_null($this->next_cursor)) {
74
            $this->fetchNext();
75
76
            return $this->next();
77
        } else {
78
            $this->current_index = 0;
79
80
            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...
81
        }
82
    }
83
84
    /**
85
     * @param array $params
86
     * @return Cursor
87
     */
88
    public function fetchNext($params = [])
89
    {
90
        $params['cursor'] = $this->next_cursor;
91
        switch ($this->account->getTwitterAds()->getMethod()) {
92
            case 'GET':
93
                $response = $this->account->getTwitterAds()->get($this->account->getTwitterAds()->getResource(), $params);
94
            break;
95
            case 'POST':
96
                $response = $this->account->getTwitterAds()->post($this->account->getTwitterAds()->getResource(), $params);
97
            break;
98
            case 'PUT':
99
                $response = $this->account->getTwitterAds()->put($this->account->getTwitterAds()->getResource(), $params);
100
            break;
101
            case 'DELETE':
102
                $response = $this->account->getTwitterAds()->delete($this->account->getTwitterAds()->getResource(), $params);
103
            break;
104
            default:
105
                $response = $this->account->getTwitterAds()->get($this->account->getTwitterAds()->getResource(), $params);
106
            break;
107
        }
108
109
        return $this->fromResponse($response);
110
    }
111
112
    /**
113
     * @param $request
114
     * @return $this
115
     */
116
    public function fromResponse($request)
117
    {
118
        $this->next_cursor = isset($request->next_cursor) ? $request->next_cursor : null;
119
        if (isset($request->total_count)) {
120
            $this->total_count = intval($request->total_count);
121
        }
122
        foreach ($request->data as $item) {
123
            if (method_exists($this->resource, 'fromResponse')) {
124
                if ($this->resource instanceof Account) {
125
                    $obj = new $this->resource($this->account->getTwitterAds());
126
                } else {
127
                    $obj = new $this->resource();
128
                }
129
                $obj->setAccount($this->account);
130
                $this->collection[] = $obj->fromResponse($item);
131
            } else {
132
                $this->collection[] = $item;
133
            }
134
        }
135
136
        return $this;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function getIterator()
143
    {
144
        return new \ArrayIterator($this->collection);
145
    }
146
147
    /**
148
     * @return array
149
     */
150
    public function getCollection()
151
    {
152
        return $this->collection;
153
    }
154
155
    /**
156
     * @param array $collection
157
     */
158
    public function setCollection($collection)
159
    {
160
        $this->collection = $collection;
161
    }
162
163
    /**
164
     * @return Account
165
     */
166
    public function getAccount()
167
    {
168
        return $this->account;
169
    }
170
171
    /**
172
     * @param Account $account
173
     */
174
    public function setAccount($account)
175
    {
176
        $this->account = $account;
177
    }
178
}
179