Gateway   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A sendRequest() 0 15 2
1
<?php
2
namespace mikemix\Wiziq\API;
3
4
use mikemix\Wiziq\Common\Api\Exception\CallException;
5
use mikemix\Wiziq\Common\Api\RequestInterface;
6
use mikemix\Wiziq\Common\Http\ClientInterface;
7
use mikemix\Wiziq\Http\CurlClient;
8
9
/**
10
 * Wiziq's API gateway
11
 */
12
class Gateway
13
{
14
    const URL = 'https://class.api.wiziq.com';
15
16
    /** @var Auth */
17
    private $auth;
18
19
    /** @var ClientInterface */
20
    private $client;
21
22
    /** @var string */
23
    private $endpointUrl;
24
25 2
    public function __construct(Auth $auth, ClientInterface $client = null, $endpointUrl = self::URL)
26
    {
27 2
        $this->auth        = $auth;
28 2
        $this->client      = $client ?: new CurlClient();
29 2
        $this->endpointUrl = $endpointUrl;
30 2
    }
31
32
    /**
33
     * @param RequestInterface $wiziqRequest
34
     *
35
     * @return \SimpleXMLElement
36
     *
37
     * @throws CallException If Wiziq declined the request
38
     */
39 2
    public function sendRequest(RequestInterface $wiziqRequest)
40
    {
41 2
        $url = sprintf('%s?method=%s', $this->endpointUrl, $wiziqRequest->getMethod());
42 2
        $params = $this->auth->preparePayload($wiziqRequest->getMethod(), $wiziqRequest->getParams());
43
44 2
        $rawResponse = $this->client->getResponse($url, $params);
45 2
        $xmlObject   = simplexml_load_string($rawResponse);
46 2
        $response    = new Response($xmlObject);
47
48 2
        if (!$response->isSuccess()) {
49 1
            throw CallException::from($response);
50
        }
51
52 1
        return $response->getResponse();
53
    }
54
}
55