RemoteAdapter::handleCallbacks()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
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 RemoteAdapter
21
 *
22
 * @package Smalot\Magento
23
 */
24
class RemoteAdapter implements RemoteAdapterInterface
25
{
26
    /**
27
     * @var array
28
     */
29
    protected static $defaultOptions = array(
30
        'exceptions'         => true,
31
        'connection_timeout' => 15,
32
        'keep_alive'         => true,
33
        'compression'        => true,
34
        'features'           => SOAP_SINGLE_ELEMENT_ARRAYS,
35
    );
36
37
    /**
38
     * @var string
39
     */
40
    protected $wsdl = null;
41
42
    /**
43
     * @var string
44
     */
45
    protected $apiUser = null;
46
47
    /**
48
     * @var string
49
     */
50
    protected $apiKey = null;
51
52
    /**
53
     * @var array
54
     */
55
    protected $options = null;
56
57
    /**
58
     * @var boolean
59
     */
60
    protected $autoLogin = null;
61
62
    /**
63
     * @var string
64
     */
65
    protected $sessionId = null;
66
67
    /**
68
     * @var \SoapClient
69
     */
70
    protected $soapClient = null;
71
72
    /**
73
     * @param string $path
74
     * @param string $apiUser
75
     * @param string $apiKey
76
     * @param array  $options
77
     * @param bool   $autoLogin
78
     */
79
    public function __construct($path, $apiUser, $apiKey, $options = array(), $autoLogin = true)
80
    {
81
        $this->wsdl      = rtrim($path, '/') . '/index.php/api/soap/?wsdl';
82
        $this->apiUser   = $apiUser;
83
        $this->apiKey    = $apiKey;
84
        $this->autoLogin = $autoLogin;
85
86
        $this->setOptions($options);
87
        @$this->soapClient = new \SoapClient($this->wsdl, $this->getOptions());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
88
    }
89
90
    /**
91
     *
92
     */
93
    public function __destruct()
94
    {
95
        $this->logout();
96
97
        unset($this->options);
98
        unset($this->sessionId);
99
        unset($this->soapClient);
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    public function getOptions()
106
    {
107
        return $this->options;
108
    }
109
110
    /**
111
     * @param $options
112
     *
113
     * @return $this
114
     */
115
    public function setOptions($options)
116
    {
117
        $this->options = array_merge(self::$defaultOptions, $options);
118
119
        return $this;
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    public static function getDefaultOptions()
126
    {
127
        return self::$defaultOptions;
128
    }
129
130
    /**
131
     * @param array $options
132
     */
133
    public static function setDefaultOptions($options = array())
134
    {
135
        self::$defaultOptions = $options;
136
    }
137
138
    /**
139
     * @param string $apiUser
140
     * @param string $apiKey
141
     *
142
     * @return bool
143
     * @throws \Exception
144
     */
145
    public function login($apiUser = null, $apiKey = null)
146
    {
147
        if (null === $apiUser) {
148
            $apiUser = $this->apiUser;
149
        }
150
151
        if (null === $apiKey) {
152
            $apiKey = $this->apiKey;
153
        }
154
155
        if ($this->sessionId = $this->soapClient->login($apiUser, $apiKey)) {
156
            return true;
157
        }
158
159
        return false;
160
    }
161
162
    /**
163
     * @return bool
164
     */
165
    public function ping()
166
    {
167
        $info = $this->call(new Action('core_magento.info'), false);
168
169
        return (is_object($info) || is_array($info));
170
    }
171
172
    /**
173
     * @return bool
174
     */
175
    public function logout()
176
    {
177
        if (null !== $this->sessionId) {
178
            $this->soapClient->endSession($this->sessionId);
179
            $this->sessionId = null;
180
181
            return true;
182
        }
183
184
        return false;
185
    }
186
187
    /**
188
     * @throws RemoteAdapterException
189
     */
190
    protected function checkSecurity()
191
    {
192
        if (null === $this->sessionId && $this->autoLogin) {
193
            $this->login();
194
        }
195
196
        if (null === $this->sessionId) {
197
            throw new RemoteAdapterException('Not connected.');
198
        }
199
    }
200
201
    /**
202
     * @param ActionInterface $action
203
     * @param bool            $throwsException
204
     *
205
     * @return array|false
206
     * @throws \Exception
207
     */
208
    public function call(ActionInterface $action, $throwsException = true)
209
    {
210
        try {
211
            $this->checkSecurity();
212
213
            $result = $this->soapClient->call($this->sessionId, $action->getMethod(), $action->getArguments());
214
215
            return $result;
216
217
        } catch (\Exception $e) {
218
            if ($throwsException) {
219
                throw $e;
220
            }
221
222
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Smalot\Magento\RemoteAdapterInterface::call 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...
223
        }
224
    }
225
226
    /**
227
     * @param MultiCallQueueInterface $queue
228
     * @param bool                    $throwsException
229
     *
230
     * @return array|false
231
     * @throws \Exception
232
     */
233
    public function multiCall(MultiCallQueueInterface $queue, $throwsException = true)
234
    {
235
        try {
236
            $this->checkSecurity();
237
238
            $actions = $this->getActions($queue);
239
            $results = $this->soapClient->multiCall($this->sessionId, $actions);
240
241
            $this->handleCallbacks($queue, $results);
242
243
            return $results;
244
245
        } catch (\Exception $e) {
246
            if ($throwsException) {
247
                throw $e;
248
            }
249
250
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Smalot\Magento\RemoteAdapterInterface::multiCall 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...
251
        }
252
    }
253
254
    /**
255
     * @param MultiCallQueueInterface $queue
256
     *
257
     * @return array
258
     */
259
    protected function getActions(MultiCallQueueInterface $queue)
260
    {
261
        $actions = array();
262
263
        foreach ($queue as $item) {
264
            $action = $item['action'];
265
266
            /** @var $action ActionInterface */
267
            $actions[] = array(
268
                $action->getMethod(),
269
                $action->getArguments(),
270
            );
271
        }
272
273
        return $actions;
274
    }
275
276
    /**
277
     * @param MultiCallQueueInterface $queue
278
     * @param array                   $results
279
     */
280
    protected function handleCallbacks(MultiCallQueueInterface $queue, $results)
281
    {
282
        foreach ($queue as $position => $item) {
283
            $callback = $item['callback'];
284
285
            if (is_callable($callback)) {
286
                call_user_func($callback, $results[$position]);
287
            }
288
        }
289
    }
290
291
    /**
292
     * @return string
293
     */
294
    public function getLastRequestHeaders()
295
    {
296
        return $this->soapClient->__getLastRequestHeaders();
297
    }
298
299
    /**
300
     * @return string
301
     */
302
    public function getLastRequest()
303
    {
304
        return $this->soapClient->__getLastRequest();
305
    }
306
307
    /**
308
     * @return string
309
     */
310
    public function getLastResponseHeaders()
311
    {
312
        return $this->soapClient->__getLastResponseHeaders();
313
    }
314
315
    /**
316
     * @return string
317
     */
318
    public function getLastResponse()
319
    {
320
        return $this->soapClient->__getLastResponse();
321
    }
322
}
323