Skip to content

Commit

Permalink
Merge pull request #8 from MikaFima/feature/update-mailchimp-client
Browse files Browse the repository at this point in the history
Feature/update mailchimp client
  • Loading branch information
tyx committed Oct 15, 2014
2 parents 8fb586f + 665db24 commit 1dbcbe4
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .atoum.bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
require_once __DIR__.'/vendor/autoload.php';
11 changes: 11 additions & 0 deletions .atoum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use \mageekguy\atoum;

$cliReport = $script->addDefaultReport();
$cliReport->addField(new atoum\report\fields\runner\result\logo());
$runner->addReport($cliReport);

$script->bootstrapFile(__DIR__ . DIRECTORY_SEPARATOR . '.atoum.bootstrap.php');

$runner->addTestsFromDirectory(__DIR__.'/Tests/Units');
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Symfony directories
vendor/*
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: php
php:
- 5.3
- 5.5
before_script:
- curl -s http://getcomposer.org/installer | php
- COMPOSER_ROOT_VERSION=dev-master php composer.phar install --dev
script:
- bin/atoum
notifications:
email:
- [email protected]
29 changes: 16 additions & 13 deletions Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace Rezzza\MailChimpBundle\Api;

use Rezzza\MailChimpBundle\Connection\ConnectionInterface;
use Rezzza\MailChimp\MCAPI;

/**
* Client
*
* @uses MCAPI
* @uses \Mailchimp
* @author Sébastien HOUZÉ <[email protected]>
*/
class Client extends MCAPI
class Client extends \Mailchimp
{

protected $connection;
protected $lastRequest;
protected $lastResponse;
Expand All @@ -24,9 +24,11 @@ class Client extends MCAPI
*/
public function __construct($apiKey)
{
parent::__construct($apiKey);
parent::__construct($apiKey, array(
'CURLOPT_FOLLOWLOCATION' => true,
));

$this->lastRequest = null;
$this->lastRequest = null;
$this->lastResponse = null;
}

Expand All @@ -53,10 +55,10 @@ public function getConnection()
/**
* {@inheritDoc}
*/
public function callServer($method, $params)
public function call($method, $params)
{
$request = new Request($method, $params);
$response = $this->call($request);
$request = new Request($method, $params);
$response = $this->callServer($request);

return $response->getContent();
}
Expand All @@ -68,23 +70,23 @@ public function callServer($method, $params)
*
* @return Response
*/
public function call(Request $request)
private function callServer(Request $request)
{
$this->errorMessage = null;
$this->errorCode = null;
$this->errorCode = null;

$request->setParam('apikey', $this->api_key);
$request->setParam('apikey', $this->apikey);

$response = $this->connection->execute($request);

if ($response->isError()) {
$content = $response->getContent();

$this->errorMessage = $content['error'];
$this->errorCode = $content['code'];
$this->errorCode = $content['code'];
}

$this->lastRequest = $request;
$this->lastRequest = $request;
$this->lastResponse = $response;

return $response;
Expand All @@ -109,4 +111,5 @@ public function getLastResponse()
{
return $this->lastResponse;
}

}
43 changes: 24 additions & 19 deletions Connection/HttpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

use Rezzza\MailChimpBundle\Api\Request;
use Rezzza\MailChimpBundle\Api\Response;

use Guzzle\Http\Client as HttpClient;

/**
* HTTP connection for the MailChimp API client
*/
class HttpConnection implements ConnectionInterface
{
const HTTP_CODE_CONNECTION_TIMED_OUT = 118;

const HTTP_CODE_CONNECTION_TIMED_OUT = 118;
const INTERNAL_CODE_UNKNOWN_EXCEPTION_ERROR = -100;
const INTERNAL_CODE_GENERAL_ERROR = -99;
const INTERNAL_CODE_TIMEOUT = -98;
const INTERNAL_CODE_PARSE_EXCEPTION = -101;

const API_URL = 'http://api.mailchimp.com/1.3/?output=php';
const API_URL = 'https://api.mailchimp.com/2.0/';

protected $secure;
protected $client;
Expand All @@ -45,21 +43,21 @@ public function __construct($secure = false, HttpClient $client = null)
*/
public function execute(Request $request)
{
$uri = $this->getUri($request->getMethod(), $request->getParam('apikey'));
$request = $this->client
->post($uri)
->addPostFields($request->getParams());
$uri = $this->getUri($request->getMethod(), $request->getParam('apikey'));
$httpRequest = $this->client
->post($uri, array('Content-Type' => 'application/json'))
->setBody(json_encode($request->getParams()));

$rawResponse = false;
try {
$rawResponse = $request->send();
$rawResponse = $httpRequest->send();

$response = $this->parseResponse($rawResponse);
} catch (\Exception $e) {
// unknown exception
$response = array(
'error' => 'An error occurred: ' . $e->getMessage(),
'code' => self::INTERNAL_CODE_UNKNOWN_EXCEPTION_ERROR
'code' => self::INTERNAL_CODE_UNKNOWN_EXCEPTION_ERROR
);
}

Expand All @@ -84,22 +82,28 @@ public function execute(Request $request)
*/
private function isReponseTimeout($rawResponse)
{
return $rawResponse instanceof \Guzzle\Http\Message\Response
&& self::HTTP_CODE_CONNECTION_TIMED_OUT == $rawResponse->getStatusCode();
return $rawResponse instanceof \Guzzle\Http\Message\Response && self::HTTP_CODE_CONNECTION_TIMED_OUT == $rawResponse->getStatusCode();
}

private function getUri($method, $apiKey)
{
$dc = 'us1';
if (strstr($apiKey, '-')){
list($key, $dc) = explode('-', $apiKey, 2);
if (!$dc) $dc = 'us1';
$dc = null;
if (strstr($apiKey, '-')) {
list($key, $dc) = explode('-', $apiKey, 2);
}
if (empty($dc) === true) {
$dc = 'us1';
}

$scheme = $this->secure ? 'https://' : 'http://';
$parts = parse_url(self::API_URL);
$parts = parse_url(self::API_URL);

return $scheme . $dc . '.' . $parts['host'] . $parts['path'] . '?' . $parts['query'] . '&method=' . $method;
$uri = $scheme . $dc . '.' . $parts['host'] . $parts['path'] . $method . '.php';
if (isset($parts['query']) === true) {
$uri .= '?' . $parts['query'];
}

return $uri;
}

/**
Expand Down Expand Up @@ -136,7 +140,7 @@ private function isSerialized($data)
*/
private function parseResponse($rawResponse)
{
if (false === $rawResponse ) {
if (false === $rawResponse) {
return false;
}

Expand All @@ -157,4 +161,5 @@ private function parseResponse($rawResponse)
'code' => self::INTERNAL_CODE_PARSE_EXCEPTION
);
}

}
149 changes: 149 additions & 0 deletions Tests/Units/Api/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace Rezzza\MailChimpBundle\Tests\Units\Api;

/**
* Description of HttpConnection
*
* @author mika
*/
class Client extends \mageekguy\atoum\test
{
const CUSTOMER_LIST_ID = 'db993d96da';

private $customerEmail;

/**
* Test batch-subscribe success
*/
public function testBatchSubscribeSuccess()
{
$response = $this->getClient()->call('lists/batch-subscribe', $this->getBatchSubscribeParameters());
$this->array($response)->isNotEmpty()->notHasKey('error');
}

/**
* Test batch-subscribe fail
*/
public function testBatchSubscribeFail()
{
$response = $this->getClient()->call('lists/batch-subscribe', array_merge($this->getBatchSubscribeParameters(), array('id' => 'victor_samuel_mackey')));
$this->array($response)->isNotEmpty()->hasKey('error');
}

/**
* Test batch-unsubscribe success
*/
public function testBatchUnsubscribeSuccess()
{
$response = $this->getClient()->call('lists/batch-unsubscribe', $this->getBatchUnsubscribeParameters());
$this->array($response)->isNotEmpty()->notHasKey('error');
}

/**
* Test batch-unsubscribe fail
*/
public function testBatchUnsubscribeFail()
{
$response = $this->getClient()->call('lists/batch-unsubscribe', array_merge($this->getBatchUnsubscribeParameters(), array('id' => 'victor_samuel_mackey')));
$this->array($response)->isNotEmpty()->hasKey('error');
}

/**
* Get MailChimp Client
* @return \Rezzza\MailChimpBundle\Api\Client
*/
private function getClient()
{
$client = new \Rezzza\MailChimpBundle\Api\Client('e90c270f8cf2cfbb2d5c14a36ba884c2-us9');
$client->setConnection(new \Rezzza\MailChimpBundle\Connection\HttpConnection(true));

return $client;
}

/**
* Parameters for subscribe method
* @return array
*/
private function getBatchSubscribeParameters()
{
return array(
'id' => self::CUSTOMER_LIST_ID,
'batch' => array(
array(
'email' => array('email' => $this->getEmail()),
'email_type' => $this->getEmailType(),
'merge_vars' => $this->getMergeVars(),
),
),
"double_optin" => false,
"update_existing" => true,
"replace_interests" => true
);
}

/**
* Parameters for unsubscribe method
* @return array
*/
private function getBatchUnsubscribeParameters()
{
return array(
'id' => self::CUSTOMER_LIST_ID,
'batch' => array(
array(
'email' => $this->getEmail(),
),
),
"delete_member" => false,
"send_goodbye" => false,
"send_notify" => false,
);
}

/**
* Customer email
* @return string
*/
private function getEmail()
{
if (is_null($this->customerEmail) === true) {
$this->customerEmail = 'mika+mailchimptest'.time().'@verylastroom.com';
}

return $this->customerEmail;
}

/**
* Customer email type
* @return string
*/
private function getEmailType()
{
return 'html';
}

/**
* Customer merge_vars
* @return array
*/
private function getMergeVars()
{
return array(
'FNAME' => 'john',
'LNAME' => 'doe',
'GENDER' => 'Male',
'LOCALE' => 'fr',
'SIGNUPVIA' => 'EMAIL',
'PHONE' => '0606060606',
'CREDIT' => 30,
'CREATEDAT' => date('Y-m-d H:i:s'),
'UPDATEDAT' => date('Y-m-d H:i:s'),
'APP_VER' => null,
'APP_OS' => null,
'REF_CODE' => 'JDOE',
'CREDIT_EXP' => date('Y-m-d H:i:s'),
);
}

}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}],
"require": {
"php": ">=5.3.3",
"rezzza/mailchimp": "1.3.*",
"mailchimp/mailchimp": "2.0.5",
"guzzle/http": "3.*"
},
"require-dev": {
Expand Down

0 comments on commit 1dbcbe4

Please sign in to comment.