Completed
Push — master ( 2e002a...ff3a53 )
by Mario
04:03
created

HttpClientFactory::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Marek\Toggable\Factory\Http;
4
5
use Marek\Toggable\API\Security\TokenInterface;
6
use Marek\Toggable\Factory\FactoryInterface;
7
use InvalidArgumentException;
8
use Marek\Toggable\Http\Client\NativeHttpClient;
9
use Marek\Toggable\Http\Converter\NativeArgumentsConverter;
10
use Marek\Toggable\Http\Parser\NativeResponseParser;
11
12
/**
13
 * Class HttpClientFactory
14
 * @package Marek\Toggable\Factory\Http
15
 */
16
class HttpClientFactory implements FactoryInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private $uri;
22
23
    /**
24
     * @var \Marek\Toggable\API\Security\TokenInterface
25
     */
26
    private $token;
27
28
    /**
29
     * HttpClientFactory constructor.
30
     *
31
     * @param array $config
32
     * @param \Marek\Toggable\API\Security\TokenInterface $token
33
     */
34
    public function __construct($config, TokenInterface $token)
35
    {
36
        if (empty($config['marek_toggable']['base_uri']))
37
        {
38
            throw new InvalidArgumentException('Please provide base URI.');
39
        }
40
        $this->uri = $config['marek_toggable']['base_uri'];
41
        $this->token = $token;
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function build()
48
    {
49
        if (!filter_var($this->uri, FILTER_VALIDATE_URL)) {
50
            throw new InvalidArgumentException('Please provide valid base URI.');
51
        }
52
53
        return new NativeHttpClient($this->uri, $this->token, new NativeArgumentsConverter(), new NativeResponseParser());
54
    }
55
}
56