Completed
Push — master ( 122bdb...4f8f85 )
by Jens
10:49 queued 41s
created

AdapterFactory::getAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Client\Adapter;
7
8
use GuzzleHttp\Client;
9
use Commercetools\Core\Error\InvalidArgumentException;
10
11
class AdapterFactory
12
{
13
    protected $adapters = [];
14
15 66
    public function __construct()
16
    {
17 66
        $this->register('guzzle5', '\Commercetools\Core\Client\Adapter\Guzzle5Adapter')
18 66
            ->register('guzzle6', '\Commercetools\Core\Client\Adapter\Guzzle6Adapter');
19 66
    }
20
21
    /**
22
     * @param string $name
23
     * @param string $adapterClass
24
     * @return $this
25
     */
26 66
    public function register($name, $adapterClass)
27
    {
28 66
        $this->adapters[$name] = $adapterClass;
29
30 66
        return $this;
31
    }
32
33
    /**
34
     * @internal
35
     * @param string $name
36
     * @return string
37
     */
38 66
    public function getClass($name = null)
39
    {
40 66
        if (is_null($name)) {
41 66
            if (version_compare(Client::VERSION, '6.0.0', '>=')) {
42 66
                $name = 'guzzle6';
43
            } else {
44
                $name = 'guzzle5';
45
            }
46
        }
47 66
        if (isset($this->adapters[$name])) {
48 66
            return $this->adapters[$name];
49
        }
50
51
        throw new InvalidArgumentException();
52
    }
53
54
    /**
55
     * @param $name
56
     * @param $options
57
     * @return AdapterInterface
58
     */
59 66
    public function getAdapter($name, $options)
60
    {
61 66
        $adapterClass = $this->getClass($name);
62 66
        $adapter = new $adapterClass($options);
63
64 66
        return $adapter;
65
    }
66
}
67