Passed
Push — develop ( b1701d...b569c3 )
by Jens
24:20
created

AdapterFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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