MultiCallQueue::flush()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * @file
5
 *          Magento API Client (SOAP v1).
6
 *          Allows wrappers for each call, dependencies injections
7
 *          and code completion.
8
 *
9
 * @author  Sébastien MALOT <[email protected]>
10
 * @license MIT
11
 * @url     <https://github.com/smalot/magento-client>
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace Smalot\Magento;
18
19
/**
20
 * Class MultiCallQueue
21
 *
22
 * @package Smalot\Magento
23
 */
24
class MultiCallQueue implements MultiCallQueueInterface
25
{
26
    /**
27
     * @var RemoteAdapterInterface
28
     */
29
    protected $remoteAdapter = null;
30
31
    /**
32
     * @var array
33
     */
34
    protected $queue = null;
35
36
    /**
37
     * @var int
38
     */
39
    protected $position = 0;
40
41
    /**
42
     * @param RemoteAdapterInterface $remoteAdapter
43
     */
44
    public function __construct(RemoteAdapterInterface $remoteAdapter)
45
    {
46
        $this->remoteAdapter = $remoteAdapter;
47
48
        $this->queue = array();
49
    }
50
51
    /**
52
     * @param ActionInterface $action
53
     * @param callable        $callback
54
     *
55
     * @return $this
56
     */
57
    public function addAction(ActionInterface $action, $callback = null)
58
    {
59
        $this->queue[] = array(
60
            'action'   => $action,
61
            'callback' => $callback,
62
        );
63
64
        return $this;
65
    }
66
67
    /**
68
     * @return $this
69
     */
70
    public function flush()
71
    {
72
        $this->queue = array();
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    public function execute()
81
    {
82
        $results = $this->remoteAdapter->multiCall($this);
83
84
        $this->flush();
85
86
        return $results;
87
    }
88
89
    /**
90
     * (PHP 5 &gt;= 5.0.0)<br/>
91
     * Return the current element
92
     *
93
     * @link http://php.net/manual/en/iterator.current.php
94
     * @return mixed Can return any type.
95
     */
96
    public function current()
97
    {
98
        return $this->queue[$this->position];
99
    }
100
101
    /**
102
     * (PHP 5 &gt;= 5.0.0)<br/>
103
     * Move forward to next element
104
     *
105
     * @link http://php.net/manual/en/iterator.next.php
106
     * @return void Any returned value is ignored.
107
     */
108
    public function next()
109
    {
110
        $this->position++;
111
    }
112
113
    /**
114
     * (PHP 5 &gt;= 5.0.0)<br/>
115
     * Return the key of the current element
116
     *
117
     * @link http://php.net/manual/en/iterator.key.php
118
     * @return integer scalar on success, or null on failure.
119
     */
120
    public function key()
121
    {
122
        return $this->position;
123
    }
124
125
    /**
126
     * (PHP 5 &gt;= 5.0.0)<br/>
127
     * Checks if current position is valid
128
     *
129
     * @link http://php.net/manual/en/iterator.valid.php
130
     * @return boolean The return value will be casted to boolean and then evaluated.
131
     *       Returns true on success or false on failure.
132
     */
133
    public function valid()
134
    {
135
        return array_key_exists($this->position, $this->queue);
136
    }
137
138
    /**
139
     * (PHP 5 &gt;= 5.0.0)<br/>
140
     * Rewind the Iterator to the first element
141
     *
142
     * @link http://php.net/manual/en/iterator.rewind.php
143
     * @return void Any returned value is ignored.
144
     */
145
    public function rewind()
146
    {
147
        $this->position = 0;
148
    }
149
150
    /**
151
     * (PHP 5 &gt;= 5.0.0)<br/>
152
     * Whether a offset exists
153
     *
154
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
155
     *
156
     * @param mixed $offset <p>
157
     *                      An offset to check for.
158
     *                      </p>
159
     *
160
     * @return boolean true on success or false on failure.
161
     * </p>
162
     * <p>
163
     * The return value will be casted to boolean if non-boolean was returned.
164
     */
165
    public function offsetExists($offset)
166
    {
167
        return array_key_exists($offset, $this->queue);
168
    }
169
170
    /**
171
     * (PHP 5 &gt;= 5.0.0)<br/>
172
     * Offset to retrieve
173
     *
174
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
175
     *
176
     * @param mixed $offset <p>
177
     *                      The offset to retrieve.
178
     *                      </p>
179
     *
180
     * @return mixed Can return all value types.
181
     */
182
    public function offsetGet($offset)
183
    {
184
        return $this->queue[$offset];
185
    }
186
187
    /**
188
     * (PHP 5 &gt;= 5.0.0)<br/>
189
     * Offset to set
190
     *
191
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
192
     *
193
     * @param mixed $offset <p>
194
     *                      The offset to assign the value to.
195
     *                      </p>
196
     * @param mixed $value  <p>
197
     *                      The value to set.
198
     *                      </p>
199
     *
200
     * @return void
201
     */
202
    public function offsetSet($offset, $value)
203
    {
204
        $this->queue[$offset] = $value;
205
    }
206
207
    /**
208
     * (PHP 5 &gt;= 5.0.0)<br/>
209
     * Offset to unset
210
     *
211
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
212
     *
213
     * @param mixed $offset <p>
214
     *                      The offset to unset.
215
     *                      </p>
216
     *
217
     * @return void
218
     */
219
    public function offsetUnset($offset)
220
    {
221
        unset($this->queue[$offset]);
222
    }
223
224
    /**
225
     * (PHP 5 &gt;= 5.1.0)<br/>
226
     * Count elements of an object
227
     *
228
     * @link http://php.net/manual/en/countable.count.php
229
     * @return int The custom count as an integer.
230
     *       </p>
231
     *       <p>
232
     *       The return value is cast to an integer.
233
     */
234
    public function count()
235
    {
236
        return count($this->queue);
237
    }
238
}
239