forked from cordoval/MailChimpBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.php
114 lines (94 loc) · 2.32 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace Jirafe\Bundle\MailChimpBundle;
use Jirafe\Bundle\MailChimpBundle\Connection\ConnectionInterface;
use Jirafe\Bundle\MailChimpBundle\Connection\HttpConnection;
use Mailchimp\MCAPI;
class Client extends MCAPI
{
protected $connection;
protected $lastRequest;
protected $lastResponse;
/**
* Constructor
*
* @param string $apiKey
* @param Connection $connection
*/
public function __construct($apiKey, ConnectionInterface $connection = null)
{
parent::__construct($apiKey);
if (null === $connection) {
$connection = new HttpConnection();
}
$this->connection = $connection;
$this->lastRequest = null;
$this->lastResponse = null;
}
/**
* Defines the connection
*
* @param Connection $connection
*/
public function setConnection(ConnectionInterface $connection)
{
$this->connection = $connection;
}
/**
* Returns the connection
*
* @return Connection
*/
public function getConnection()
{
return $this->connection;
}
/**
* {@inheritDoc}
*/
public function callServer($method, $params)
{
$request = new Request($method, $params);
$response = $this->call($request);
return $response->getContent();
}
/**
* Perform the given Request
*
* @param Request $request
*
* @return Response
*/
public function call(Request $request)
{
$this->errorMessage = null;
$this->errorCode = null;
$request->setParam('apikey', $this->api_key);
$response = $this->connection->execute($request);
if ($response->isError()) {
$content = $response->getContent();
$this->errorMessage = $content["error"];
$this->errorCode = $content["code"];
}
$this->lastRequest = $request;
$this->lastResponse = $response;
return $response;
}
/**
* Returns the last request
*
* @return Request
*/
public function getLastRequest()
{
return $this->lastRequest;
}
/**
* Returns the last response
*
* @return Response
*/
public function getLastResponse()
{
return $this->lastResponse;
}
}