Completed
Pull Request — master (#71)
by Cassio
01:52 queued 15s
created

Client   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 53
ccs 12
cts 16
cp 0.75
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A get() 0 10 2
A post() 0 18 2
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
                $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