RestAdapter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace DICIT\Activators\Remote;
4
5
use ProxyManager\Factory\RemoteObject\AdapterInterface;
6
7
class RestAdapter implements AdapterInterface
8
{
9
10
    /**
11
     *
12
     * @var \Guzzle\Http\ClientInterface
13
     */
14
    private $client;
15
16
    private $auth = array();
17
18
    public function __construct(\Guzzle\Http\ClientInterface $client, array $auth = array())
19
    {
20
        $this->client = $client;
21
        $this->auth = $auth;
22
    }
23
24
    public function call($wrappedClass, $method, array $params = array())
25
    {
26
        $request = $this->client->post(sprintf('/%s/%s', str_replace('\\', '/', $wrappedClass), $method), $params);
27
28
        $auth = $this->getAuthConfig($this->auth);
29
        if ($auth) {
30
            $request->setAuth($auth['login'], $auth['pass'], $auth['type']);
31
        }
32
33
        $response = $request->send();
34
35
        return $response->getBody();
36
    }
37
38
    private function getAuthConfig(array $config = array())
39
    {
40
        if (count($config)) {
41
            $ret = array();
42
            $ret['type'] = $this->get($config, 'type', 'basic');
43
            $ret['login'] = $this->get($config, 'login', '');
44
            $ret['pass'] = $this->get($config, 'pass', '');
45
            return $ret;
46
        }
47
        else {
48
            return null;
49
        }
50
    }
51
52
    private function get(array $data, $key, $defaultValue = '')
53
    {
54
        if (array_key_exists($key, $data)) {
55
            return $data[$key];
56
        }
57
58
        return $defaultValue;
59
    }
60
}
61