Passed
Push — develop ( 38ffd9...9f7101 )
by Jens
31:57 queued 05:22
created

AdapterFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
wmc 8
eloc 19
dl 0
loc 56
ccs 17
cts 21
cp 0.8095
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAdapter() 0 6 1
A register() 0 5 1
A getClass() 0 16 5
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 79
    public function __construct()
17
    {
18 79
        $this->register('guzzle5', Guzzle5Adapter::class)
19 79
            ->register('guzzle6', Guzzle6Adapter::class);
20 79
    }
21
22
    /**
23
     * @param string $name
24
     * @param string $adapterClass
25
     * @return $this
26
     */
27 79
    public function register($name, $adapterClass)
28
    {
29 79
        $this->adapters[$name] = $adapterClass;
30
31 79
        return $this;
32
    }
33
34
    /**
35
     * @internal
36
     * @param string $name
37
     * @return string
38
     */
39 79
    public function getClass($name = null)
40
    {
41 79
        if (is_null($name)) {
42 79
            if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
43 79
                $name = 'guzzle6';
44
            } elseif (version_compare(Client::class . '::VERSION', '6.0.0', '>=')) {
45
                $name = 'guzzle6';
46
            } else {
47
                $name = 'guzzle5';
48
            }
49
        }
50 79
        if (isset($this->adapters[$name])) {
51 79
            return $this->adapters[$name];
52
        }
53
54
        throw new InvalidArgumentException();
55
    }
56
57
    /**
58
     * @param $name
59
     * @param $options
60
     * @return AdapterInterface
61
     */
62 79
    public function getAdapter($name, $options)
63
    {
64 79
        $adapterClass = $this->getClass($name);
65 79
        $adapter = new $adapterClass($options);
66
67 79
        return $adapter;
68
    }
69
}
70