Completed
Pull Request — master (#56)
by
unknown
07:46
created

Client::post()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 7
cts 9
cp 0.7778
rs 9.4285
cc 2
eloc 11
nc 3
nop 2
crap 2.0438
1
<?php
2
namespace PHPSC\PagSeguro\Client;
3
4
use GuzzleHttp\Client as HttpClient;
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Exception\RequestException;
7
use SimpleXMLElement;
8
9
/**
10
 * @author Luís Otávio Cobucci Oblonczyk <[email protected]>
11
 */
12
class Client
13
{
14
    private $client;
15
16
    /**
17
     * @param ClientInterface $client
18
     */
19 3
    public function __construct(ClientInterface $client = null)
20
    {
21 3
        $this->client = $client ?: new HttpClient();
22 3
    }
23
24
    /**
25
     * @param string $url
26
     * @param SimpleXMLElement $body
27
     *
28
     * @return SimpleXMLElement
29
     */
30 1
    public function post($url, SimpleXMLElement $body)
31
    {
32
        try {
33 1
            $response = $this->client->request(
34 1
                'POST',
35 1
                $url,
36
                [
37 1
                    'headers' => ['Content-Type' => 'application/xml; charset=UTF-8'],
38 1
                    'body'    => $body->asXML(),
39
                    'verify'  => false
40
                ]
41
            );
42
43 1
            return new SimpleXMLElement($response->getBody());
44
        } catch (RequestException $e) {
45
            throw PagSeguroException::create($e->getResponse(), $e);
0 ignored issues
show
Bug introduced by
It seems like $e->getResponse() can be null; however, create() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
46
        }
47
    }
48
49
    /**
50
     * @param string $url
51
     *
52
     * @return SimpleXMLElement
53
     */
54 1
    public function get($url)
55
    {
56
        try {
57 1
            $response = $this->client->request('GET', $url, ['verify' => false]);
58
59 1
            return new SimpleXMLElement($response->getBody());
60
        } catch (RequestException $e) {
61
            throw PagSeguroException::create($e->getResponse(), $e);
0 ignored issues
show
Bug introduced by
It seems like $e->getResponse() can be null; however, create() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
62
        }
63
    }
64
}
65