YourMembershipClient::makeCall()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
3
namespace P2A\YourMembership;
4
5
use GuzzleHttp\Client;
6
use P2A\YourMembership\Core\Request;
7
use P2A\YourMembership\Core\Response;
8
use P2A\YourMembership\Exceptions\YourMembershipResponseException;
9
10
class YourMembershipClient
11
{
12
13
	/**
14
	 * Guzzle Client
15
	 * @var \GuzzleHttp\Client
16
	 */
17
	private $client;
18
	/**
19
	 * YourMembership Request
20
	 * @var Request
21
	 */
22
	private $request;
23
24 2
	public function __construct(Client $client, string $apiKey, string $saPasscode)
25
	{
26 2
		$this->client = $client;
27 2
		$this->request = new Request($apiKey, $saPasscode);
28 2
		$sessionID = $this->createSession();
29 2
		Request::setSessionID($sessionID);
30
31 2
	}
32
	/**
33
	 * Makes API Call to YourMembership
34
	 * @method makeCall
35
	 * @author PA
36
	 * @author AB http://github.com/chefboyarsky
37
	 * @date   2017-01-10
38
	 * @param  string     $method    Your Membership API Method
39
	 * @param  array      $arguments  Your Membership API Call Arguments
40
	 * @return Response
41
	 * @throws YourMembershipResponseException If the response from the API has an error
42
	 */
43 2
	public function makeCall(string $method, array $arguments = [])
44
	{
45
46 2
		Request::$callId++; //Update the Call ID as they need to be unique per call
47 2
		$request = $this->request->buildRequest($method, $arguments);
48
49 2
		$response = new Response($method, $this->client->send($request));
50
51 2
		if($response->hasError())
52
		{
53 1
			throw new YourMembershipResponseException($response->getError(), $response->getErrorCode(), $method);
54
		}
55
56 2
		return $response;
57
	}
58
59
	/**
60
	 * Creates a new Session with YourMembership
61
	 * @method createSession
62
	 * @author PA
63
	 * @date   2017-01-10
64
	 * @return string        SessionID
65
	 */
66 2
	private function createSession() : string
67
	{
68 2
		$response = $this->makeCall('Session.Create')->toObject();
69 2
		return $response->SessionID;
70
71
	}
72
73
}
74