Completed
Push — master ( f87b82...3f7aa1 )
by Mario
02:46
created

NativeRequestManager::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.243

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 18
ccs 7
cts 10
cp 0.7
rs 9.4285
cc 3
eloc 12
nc 2
nop 4
crap 3.243
1
<?php
2
3
namespace Marek\Toggable\Http\Manager;
4
5
use Marek\Toggable\API\Exception\Http\NetworkException;
6
use Marek\Toggable\API\Exception\Http\ServerException;
7
use Marek\Toggable\API\Http\Response\Response;
8
use InvalidArgumentException;
9
use Marek\Toggable\Http\Parser\HttpResponseParserInterface;
10
11
/**
12
 * Class NativeRequestManager
13
 * @package Marek\Toggable\Http\Manager
14
 */
15
class NativeRequestManager implements RequestManagerInterface
16
{
17
    /**
18
     * @var \Marek\Toggable\Http\Client\HttpClientInterface
19
     */
20
    private $client;
21
22
    /**
23
     * @var \Marek\Toggable\API\Security\TokenInterface
24
     */
25
    private $token;
26
27
    /**
28
     * @var string
29
     */
30
    private $uri;
31
    /**
32
     * @var \Marek\Toggable\Http\Parser\HttpResponseParserInterface
33
     */
34
    private $parser;
35
36
    /**
37
     * NativeRequestManager constructor.
38
     *
39
     * @param \Marek\Toggable\Http\Client\HttpClientInterface $client
40
     * @param \Marek\Toggable\API\Security\TokenInterface $token
41
     * @param \Marek\Toggable\Http\Parser\HttpResponseParserInterface $parser
42
     * @param string $uri
43
     */
44 14
    public function __construct(
45
        \Marek\Toggable\Http\Client\HttpClientInterface $client,
46
        \Marek\Toggable\API\Security\TokenInterface $token,
47
        \Marek\Toggable\Http\Parser\HttpResponseParserInterface $parser,
48
        $uri
49
    )
50
    {
51 14
        $this->client = $client;
52 14
        $this->token = $token;
53
54 14
        if (empty($uri) || !is_string($uri)) {
55
            throw new InvalidArgumentException(
56
                sprintf('Please provide valid ur in %s', get_class($this))
57
            );
58
        }
59 14
        $this->uri = $uri;
60 14
        $this->parser = $parser;
61 14
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function request(\Marek\Toggable\API\Http\Request\Request $request)
67
    {
68
        $opts = array(
69
            'http' => array(
70
                'method' => $request->getMethod(),
71
                'header' => array(
72
                    'Content-type: application/json',
73
                ),
74
            ),
75
        );
76
77
        $auth = $this->token->getAuthentication();
78
        $auth = $auth[0] . ':' . $auth[1];
79
        $auth = base64_encode($auth);
80
        $opts['http']['header'][] = 'Authorization: Basic ' . $auth;
81
82
        if (!empty($request->getData())) {
83
            $opts['http']['content'] = json_encode($request);
84
        }
85
86
        $uri = $this->uri . '/' . $request->getUri();
87
88
        $data = $this->client->send($uri, $opts);
89
90
        return $this->parser->parse($data);
91
    }
92
}
93