Skip to content

Commit

Permalink
Update and fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ALTELMA committed May 15, 2019
1 parent 51a4259 commit eba9f43
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 29 deletions.
19 changes: 13 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"authors": [
{
"name": "ALTELMA",
"email": "phongthorn.a@gmail.com"
"email": "nrohtgnohp@gmail.com"
}
],
"require": {
Expand All @@ -21,12 +21,19 @@
"autoload": {
"psr-4": {
"Altelma\\LaravelMailChimp\\": "src/"
}
},
"files": [
"src/helpers.php"
]
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"laravel": {
"providers": [
"Altelma\\JWT\\JWTServiceProvider"
],
"aliases": {
"JWT": "Altelma\\JWT\\MailChimpFacade"
}
}
},
"minimum-stability": "dev"
}
}
5 changes: 4 additions & 1 deletion config/mailchimp.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

return [
'api-key' => '',
'apiKey' => '',
'lists' => [
'newsletter' => ''
]
];
62 changes: 47 additions & 15 deletions src/MailChimpService.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,84 @@
<?php

namespace LaravelMailChimp;
namespace Altelma\LaravelMailChimp;

class MailChimpService
{
private $apikey;
private $apiKey;
private $endpoint = 'https://us1.api.mailchimp.com/3.0/';

public function __construct($apikey = '')
public function __construct(string $apiKey = '')
{
$this->apikey = $apikey;
$this->getEndpoint($apikey);
$this->apiKey = $apiKey;
$this->getEndpoint($apiKey);
}

private function getEndpoint($apikey)
/**
* @param string $apiKey
*/
private function getEndpoint(string $apiKey)
{
// if (!strpos($apikey, 'us1') !== FALSE) {
$dc = explode('-', $apikey)[1];
$dc = explode('-', $apiKey)[1];
$this->endpoint = str_replace('us1', $dc, $this->endpoint);
// }
}

public function get($method, $args = [])
/**
* @param string $method
* @param array $args
* @return mixed
*/
public function get(string $method, array $args = [])
{
return $this->makeRequest('get', $method, $args);
}

public function post($method, $args = [])
/**
* @param string $method
* @param array $args
* @return mixed
*/
public function post(string $method, array $args = [])
{
return $this->makeRequest('post', $method, $args);
}

public function patch($method, $args = [])
/**
* @param string $method
* @param array $args
* @return mixed
*/
public function patch(string $method, array $args = [])
{
return $this->makeRequest('patch', $method, $args);
}

/**
* @param $method
* @param array $args
* @return mixed
*/
public function put($method, $args = [])
{
return $this->makeRequest('put', $method, $args);
}

public function delete($method, $args = [])
/**
* @param string $method
* @param array $args
* @return mixed
*/
public function delete(string $method, array $args = [])
{
return $this->makeRequest('delete', $method, $args);
}

private function makeRequest($request, $method, $args = [])
/**
* @param string $request
* @param string $method
* @param array $args
* @return mixed
*/
private function makeRequest(string $request, string $method, array $args = [])
{
$url = $this->endpoint.$method;
$json_data = json_encode($args, JSON_FORCE_OBJECT);
Expand All @@ -56,7 +88,7 @@ private function makeRequest($request, $method, $args = [])
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/vnd.api+json',
'Content-Type: application/vnd.api+json',
'Authorization: apikey '.$this->apikey, ]);
'Authorization: apikey '.$this->apiKey, ]);
// curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
Expand Down
7 changes: 5 additions & 2 deletions src/MailChimpServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ public function register()
$this->mergeConfigFrom(__DIR__.'/../config/mailchimp.php', 'mailchimp');
$this->app->singleton('MailChimp', function ($app) {
$config = $app->make('config');
$token = $config->get('mailchimp.apikey');
$token = $config->get('mailchimp.apiKey');

return new MailChimpService($token);
});
}

/**
* @return array
*/
public function provides()
{
return ['MailChimp'];
return ['mailchimp'];
}
}
5 changes: 0 additions & 5 deletions src/config/mailchimp.php

This file was deleted.

14 changes: 14 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

if (!function_exists('config_path')) {
/**
* Get the configuration path.
*
* @param string $path
* @return string
*/
function config_path($path = '')
{
return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
}
}

0 comments on commit eba9f43

Please sign in to comment.