Completed
Push — develop ( 991012...b60d88 )
by Jens
08:17
created

AdapterFactory::getClass()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.25
Metric Value
dl 0
loc 15
ccs 6
cts 8
cp 0.75
rs 9.2
cc 4
eloc 9
nc 6
nop 1
crap 4.25
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 204
    public function __construct()
16
    {
17 204
        $this->register('guzzle5', '\Commercetools\Core\Client\Adapter\Guzzle5Adapter')
18 204
            ->register('guzzle6', '\Commercetools\Core\Client\Adapter\Guzzle6Adapter');
19 204
    }
20
21
    /**
22
     * @param string $name
23
     * @param string $adapterClass
24
     * @return $this
25
     */
26 204
    public function register($name, $adapterClass)
27
    {
28 204
        $this->adapters[$name] = $adapterClass;
29
30 204
        return $this;
31
    }
32
33
    /**
34
     * @param string $name
35
     * @return string
36
     */
37 204
    public function getClass($name = null)
38
    {
39 204
        if (is_null($name)) {
40 204
            if (version_compare(Client::VERSION, '6.0.0', '>=')) {
41 204
                $name = 'guzzle6';
42
            } else {
43
                $name = 'guzzle5';
44
            }
45
        }
46 204
        if (isset($this->adapters[$name])) {
47 204
            return $this->adapters[$name];
48
        }
49
50
        throw new InvalidArgumentException();
51
    }
52
}
53