Completed
Push — master ( c93e0a...f87b82 )
by Mario
09:30 queued 03:59
created

NativeRequestManager::request()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 33
ccs 0
cts 12
cp 0
rs 8.5806
cc 4
eloc 19
nc 2
nop 1
crap 20
1
<?php
2
3
namespace Marek\Toggable\Http\Manager;
4
5
use Marek\Toggable\API\Http\Response\Response;
6
use InvalidArgumentException;
7
8
/**
9
 * Class NativeRequestManager
10
 * @package Marek\Toggable\Http\Manager
11
 */
12
class NativeRequestManager implements RequestManagerInterface
13
{
14
    /**
15
     * @var \Marek\Toggable\Http\Client\HttpClientInterface
16
     */
17
    private $client;
18
19
    /**
20
     * @var \Marek\Toggable\API\Security\TokenInterface
21
     */
22
    private $token;
23
24
    /**
25
     * @var string
26
     */
27
    private $uri;
28
29
    /**
30
     * NativeRequestManager constructor.
31
     *
32
     * @param \Marek\Toggable\Http\Client\HttpClientInterface $client
33
     * @param \Marek\Toggable\API\Security\TokenInterface $token
34
     * @param string $uri
35
     */
36 14
    public function __construct(
37
        \Marek\Toggable\Http\Client\HttpClientInterface $client,
38
        \Marek\Toggable\API\Security\TokenInterface $token,
39
        $uri
40
    )
41
    {
42 14
        $this->client = $client;
43 14
        $this->token = $token;
44
45 14
        if (empty($uri) || !is_string($uri)) {
46
            throw new InvalidArgumentException(
47
                sprintf('Please provide valid ur in %s', get_class($this))
48
            );
49
        }
50 14
        $this->uri = $uri;
51 14
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function request(\Marek\Toggable\API\Http\Request\Request $request)
57
    {
58
        $opts = array(
59
            'http' => array(
60
                'method' => $request->getMethod(),
61
                'header' => array(
62
                    'Content-type: application/json',
63
                ),
64
            ),
65
        );
66
67
        $auth = $this->token->getAuthentication();
68
        $auth = $auth[0] . ':' . $auth[1];
69
        $auth = base64_encode($auth);
70
        $opts['http']['header'][] = 'Authorization: Basic ' . $auth;
71
72
        if (!empty($request->getData())) {
73
            $opts['http']['content'] = json_encode($request);
74
        }
75
76
        $uri = $this->uri . '/' . $request->getUri();
77
78
        $data = $this->client->send($uri, $opts);
79
80
        var_dump(json_decode($data['data'], true));die;
0 ignored issues
show
Security Debugging Code introduced by
var_dump(json_decode($data['data'], true)); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
Coding Style Compatibility introduced by
The method request() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
81
        if (empty($data)) {
0 ignored issues
show
Unused Code introduced by
if (empty($data)) { throw new \Exception(); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
Bug introduced by
The variable $data seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
82
            throw new \Exception;
83
        }
84
85
        if ($data['data'] === false) {
86
            throw new \Exception;
87
        }
88
    }
89
}
90