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

AdapterFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 89.47%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 56
ccs 17
cts 19
cp 0.8947
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A register() 0 6 1
A getClass() 0 15 4
A getAdapter() 0 7 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