Skip to content
This repository has been archived by the owner on Nov 30, 2017. It is now read-only.

Start support MailChimp API v2. #34

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
.DS_Store
.DS_Store
composer.lock
2 changes: 1 addition & 1 deletion Resources/meta/LICENSE → LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2011 Miguel Perez
Copyright (c) 2014 Miguel Perez

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
25 changes: 16 additions & 9 deletions Services/HttpClient.php → Lib/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
* with this source code in the file LICENSE.
*/

namespace MZ\MailChimpBundle\Services;
namespace MZ\MailChimpBundle\Lib;

use Buzz\Browser,
Buzz\Client\Curl;
Use \Curl;

/**
* HTTP client
* HTTP Client
*
* @author Miguel Perez <[email protected]>
*/
Expand Down Expand Up @@ -58,12 +57,20 @@ protected function makeRequest($apiCall, $payload, $export = false)
} else {
$url = $this->dataCenter . '1.3/?method=' . $apiCall;
}
$curl = new Curl();
$curl->setOption(CURLOPT_USERAGENT, 'MZMailChimpBundle');
$browser = new Browser($curl);
$response = $browser->post($url, array(), http_build_query($payload));

return $response->getContent();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "MZMailChimpBundle");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));

$result = curl_exec($ch);
curl_close($ch);

var_dump($result);

return $result;
}

}
6 changes: 3 additions & 3 deletions Services/MailChimp.php → Lib/v1/MailChimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* with this source code in the file LICENSE.
*/

namespace MZ\MailChimpBundle\Services;
namespace MZ\MailChimpBundle\Lib\v1;

/**
* Mailchimp
Expand All @@ -31,12 +31,12 @@ class MailChimp
*/
public function __construct($apiKey, $listId, $ssl = true)
{
trigger_error("API v1 has being deprecated by MailChimp. A new version of this bundle is being work on.", E_USER_DEPRECATED);

$this->apiKey = $apiKey;
$this->listId = $listId;

$key = preg_split("/-/", $this->apiKey);

trigger_error("API v1 has being deprecated by MailChimp. Please use `Mailchimp.v2` service.", E_USER_DEPRECATED);

if($ssl) {
$this->dataCenter ='https://' . $key[1] . '.api.mailchimp.com/';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* with this source code in the file LICENSE.
*/

namespace MZ\MailChimpBundle\Services\Methods;
namespace MZ\MailChimpBundle\Lib\v1\Methods;

use MZ\MailChimpBundle\Services\HttpClient;
use MZ\MailChimpBundle\Lib\HttpClient;

/**
* Mailchimp Campagin method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* with this source code in the file LICENSE.
*/

namespace MZ\MailChimpBundle\Services\Methods;
namespace MZ\MailChimpBundle\Lib\v1\Methods;

use MZ\MailChimpBundle\Services\HttpClient;
use MZ\MailChimpBundle\Lib\HttpClient;

/**
* Mailchimp Ecommerce methods
Expand Down
4 changes: 2 additions & 2 deletions Services/Methods/MCExport.php → Lib/v1/Methods/MCExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* with this source code in the file LICENSE.
*/

namespace MZ\MailChimpBundle\Services\Methods;
namespace MZ\MailChimpBundle\Lib\v1\Methods;

use MZ\MailChimpBundle\Services\HttpClient;
use MZ\MailChimpBundle\Lib\HttpClient;

/**
* Mailchimp Export api
Expand Down
4 changes: 2 additions & 2 deletions Services/Methods/MCList.php → Lib/v1/Methods/MCList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* with this source code in the file LICENSE.
*/

namespace MZ\MailChimpBundle\Services\Methods;
namespace MZ\MailChimpBundle\Lib\v1\Methods;

use MZ\MailChimpBundle\Services\HttpClient;
use MZ\MailChimpBundle\Lib\HttpClient;

/**
* Mailchimp List method
Expand Down
98 changes: 98 additions & 0 deletions Lib/v2/MailChimp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/*
* This file is part of the MZ\MailChimpBundle
*
* (c) Miguel Perez <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace MZ\MailChimpBundle\Lib\v2;

/**
* Mailchimp
*
* @author Miguel Perez <[email protected]>
*/
class MailChimp
{

private $apiKey;
private $listId;
private $dataCenter;

/**
* Initializes MailChimp
*
* @param string $apiKey Mailchimp api key
* @param string $listId Default mailing list id
*/
public function __construct($apiKey, $listId, $ssl = true)
{
$this->apiKey = $apiKey;
$this->listId = $listId;

$key = $this->setDataCenter($apiKey);

if($ssl) {
$this->dataCenter ='https://' . $key[1] . '.api.mailchimp.com/2.0/';
}else {
$this->dataCenter ='http://' . $key[1] . '.api.mailchimp.com/2.0/';
}

if (!function_exists('curl_init')) {
throw new \Exception('This bundle needs the cURL PHP extension.');
}
}

/**
* Get Mailchimp api key
*
* @return string
*/
public function getAPIkey()
{
return $this->apiKey;
}

/**
* Set mailing list id
*
* @param string $listId mailing list id
*/
public function setListID($listId)
{
$this->listId = $listId;
}

/**
* get mailing list id
*
* @return string $listId
*/
public function getListID()
{
return $this->listId;
}

/**
* Set datacenter
*
* @param string $apiKey API key
*/
private function setDataCenter($apiKey){
$this->dataCenter = preg_split("/-/", $apiKey);
}

/**
* get datacenter
*
* @return string $dataCenter
*/
public function getDataCenter()
{
return $this->dataCenter;
}
}
10 changes: 8 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
<parameter key="mz_mail_chimp.api_key">null</parameter>
<parameter key="mz_mail_chimp.default_list">null</parameter>
<parameter key="mz_mail_chimp.ssl">true</parameter>
<parameter key="mz_mail_chimp.mailchimp.service.class">MZ\MailChimpBundle\Services\MailChimp</parameter>
<parameter key="mz_mail_chimp.mailchimp.lib.v1.class">MZ\MailChimpBundle\Lib\v1\MailChimp</parameter>
<parameter key="mz_mail_chimp.mailchimp.lib.v2.class">MZ\MailChimpBundle\Lib\v2\MailChimp</parameter>
</parameters>

<services>
<service id="MailChimp" class="%mz_mail_chimp.mailchimp.service.class%">
<service id="MailChimp" class="%mz_mail_chimp.mailchimp.lib.v1.class%">
<argument>%mz_mail_chimp.api_key%</argument>
<argument>%mz_mail_chimp.default_list%</argument>
<argument>%mz_mail_chimp.ssl%</argument>
</service>
<service id="MailChimp.v2" class="%mz_mail_chimp.mailchimp.lib.v2.class%">
<argument>%mz_mail_chimp.api_key%</argument>
<argument>%mz_mail_chimp.default_list%</argument>
<argument>%mz_mail_chimp.ssl%</argument>
Expand Down
Empty file removed Resources/doc/index.rst
Empty file.
66 changes: 0 additions & 66 deletions Tests/Services/MailChimpTest.php

This file was deleted.

61 changes: 0 additions & 61 deletions Tests/Services/Methods/MCExportTest.php

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"ext-curl": "*"
},
"autoload": {
"psr-0": { "MZ\\MailChimpBundle": "" }
"psr-0": { "MZ\\MailChimpBundle": "src/" }
},
"target-dir": "MZ/MailChimpBundle"
}