Completed
Pull Request — develop (#48)
by Hugo
04:15 queued 01:59
created

Client   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A post() 0 18 2
A get() 0 9 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
    public function __construct(ClientInterface $client = null)
20
    {
21
        $this->client = $client ?: new HttpClient();
22 6
    }
23
24 6
    /**
25 6
     * @param string $url
26 6
     * @param SimpleXMLElement $body
27
     *
28
     * @return SimpleXMLElement
29
     */
30
    public function post($url, SimpleXMLElement $body)
31
    {
32
        try {
33 2
            $response = $this->client->request(
34
                'POST',
35 2
                $url,
36 1
                [
37
                    'headers' => ['Content-Type' => 'application/xml; charset=UTF-8'],
38
                    'body'    => $body->asXML(),
39 1
                    'verify'  => false
40
                ]
41
            );
42
43
            return new SimpleXMLElement($response->getBody());
44
        } catch (RequestException $e) {
45
            throw new PagSeguroException($e->getMessage());
46
        }
47
    }
48 1
49
    /**
50 1
     * @param string $url
51 1
     *
52
     * @return SimpleXMLElement
53 1
     */
54 1
    public function get($url)
55
    {
56 1
        try {
57 1
            $response = $this->client->request('GET', $url, ['verify' => false]);
58
            return new SimpleXMLElement($response->getBody());
59 1
        } catch (RequestException $e) {
60
            throw new PagSeguroException($e->getMessage());
61
        }
62
    }
63
}
64