Skip to content

Commit

Permalink
PHP8 update
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexej Krzewitzki committed Dec 22, 2021
1 parent 9d34d75 commit 35d9c8f
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 123 deletions.
17 changes: 14 additions & 3 deletions src/LaravelCM/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Flobbos\LaravelCM;

use Flobbos\LaravelCM\Contracts\BaseClientContract;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
use Exception;
use Flobbos\LaravelCM\Contracts\BaseClientContract;
use Flobbos\LaravelCM\Exceptions\ConfigKeyNotSetException;

abstract class BaseClient implements BaseClientContract
Expand All @@ -29,12 +29,23 @@ public function __construct()
}


/**
* @inheritDoc
*
* @param array $options
* @return self
*/
public function setOptions(array $options): self
{
$this->options = $options;
return $this;
}

/**
* @inheritDoc
*
* @return array
*/
public function getOptions(): array
{
return $this->options;
Expand Down Expand Up @@ -194,7 +205,7 @@ public function callApi(): \GuzzleHttp\Client
return $this->guzzle;
}

public function makeCall($method = 'get', $url, array $request_data)
public function makeCall($url, array $request_data, $method = 'get')
{
try {
return $this->formatResult(
Expand Down
125 changes: 69 additions & 56 deletions src/LaravelCM/Campaigns.php
Original file line number Diff line number Diff line change
@@ -1,127 +1,140 @@
<?php

namespace Flobbos\LaravelCM;

use Flobbos\LaravelCM\Contracts\CampaignContract;
use Flobbos\LaravelCM\BaseClient;
use Exception;
use Flobbos\LaravelCM\Exceptions\MethodNotFoundException;

class Campaigns extends BaseClient implements CampaignContract{

class Campaigns extends BaseClient implements CampaignContract
{

use Traits\ResultFormat;
protected $skip_key = 'listID';

public function getDrafts() {
$drafts = $this->makeCall('get','clients/'.$this->getClientID().'/drafts',[]);
if($drafts->get('code') != '200'){

public function getDrafts()
{
$drafts = $this->makeCall('clients/' . $this->getClientID() . '/drafts', []);
if ($drafts->get('code') != '200') {
throw new Exception($drafts->get('body'));
}
return collect($drafts->get('body'));
}

public function getScheduled() {
$drafts = $this->makeCall('get','clients/'.$this->getClientID().'/scheduled',[]);
if($drafts->get('code') != '200'){

public function getScheduled()
{
$drafts = $this->makeCall('clients/' . $this->getClientID() . '/scheduled', []);
if ($drafts->get('code') != '200') {
throw new Exception($drafts->get('body'));
}
return collect($drafts->get('body'));
}

public function getSent() {
$drafts = $this->makeCall('get','clients/'.$this->getClientID().'/campaigns',[]);
if($drafts->get('code') != '200'){

public function getSent()
{
$drafts = $this->makeCall('clients/' . $this->getClientID() . '/campaigns', []);
if ($drafts->get('code') != '200') {
throw new Exception($drafts->get('body'));
}
return collect($drafts->get('body'));
}

public function getCampaignSummary($campaign_id) {
$summary = $this->makeCall('get','campaigns/'.$campaign_id.'/summary',[]);
if($summary->get('code') != '200'){

public function getCampaignSummary($campaign_id)
{
$summary = $this->makeCall('campaigns/' . $campaign_id . '/summary', []);
if ($summary->get('code') != '200') {
throw new Exception($summary->get('body'));
}
return $summary->get('body');
}

public function getCampaignDetails(string $campaign_id, string $type = 'drafts') {
if(!method_exists($this, 'get'. ucfirst($type))){
throw new MethodNotFoundException('get'. ucfirst($type));
public function getCampaignDetails(string $campaign_id, string $type = 'drafts')
{
if (!method_exists($this, 'get' . ucfirst($type))) {
throw new MethodNotFoundException('get' . ucfirst($type));
}
$campaigns = $this->{'get'. ucfirst($type)}();
return $campaigns->where('CampaignID',$campaign_id)->first();
$campaigns = $this->{'get' . ucfirst($type)}();
return $campaigns->where('CampaignID', $campaign_id)->first();
}

public function getEmailClientUsage($campaign_id) {
$users = $this->makeCall('get','campaigns/'.$campaign_id.'/emailclientusage',[]);
if($users->get('code') != '200'){

public function getEmailClientUsage($campaign_id)
{
$users = $this->makeCall('campaigns/' . $campaign_id . '/emailclientusage', []);
if ($users->get('code') != '200') {
throw new Exception($users->get('body'));
}
return $users->get('body');
}

public function getListsAndSegments(string $campaign_id){
$result = $this->makeCall('get','campaigns/'.$campaign_id.'/listsandsegments',[]);
if($result->get('code') != '200'){

public function getListsAndSegments(string $campaign_id)
{
$result = $this->makeCall('campaigns/' . $campaign_id . '/listsandsegments', []);
if ($result->get('code') != '200') {
throw new Exception($result->get('body'));
}
return $result->get('body');
}

public function createDraft(array $options) {

public function createDraft(array $options)
{
$options['SegmentIDs'] = [];
$data = ['json' => $options];
//dd($data);
$result = $this->makeCall('post','campaigns/'.$this->getClientID(),$data);
if($result->get('code') != '201'){
$result = $this->makeCall('campaigns/' . $this->getClientID(), $data, 'post');
if ($result->get('code') != '201') {
throw new Exception($result->get('body'));
}
//Return formatted list
return $result->get('body');
}

public function delete(string $campaign_id) {
$result = $this->makeCall('delete','campaigns/'.$campaign_id,[]);
if($result->get('code') != '200'){

public function delete(string $campaign_id)
{
$result = $this->makeCall('campaigns/' . $campaign_id, [], 'delete');
if ($result->get('code') != '200') {
throw new Exception($result->get('body'));
}
return true;
}

public function sendPreview(string $campaign_id, array $recipients = []){

public function sendPreview(string $campaign_id, array $recipients = [])
{
//dd(json_encode(['PreviewRecipients'=>$recipients]));
$result = $this->makeCall('post','campaigns/'.$campaign_id.'/sendpreview',[
'json' => ['PreviewRecipients'=>$recipients]
]);
$result = $this->makeCall('campaigns/' . $campaign_id . '/sendpreview', [
'json' => ['PreviewRecipients' => $recipients]
], 'post');
//dd($result);
if($result->get('code') != '200'){
if ($result->get('code') != '200') {
throw new Exception($result->get('body'));
}
return $result->get('body');
}

//"ConfirmationEmail": "confirmation@example.com, another@example.com",
//"SendDate": "YYYY-MM-DD HH:MM"
//https://api.createsend.com/api/v3.2/campaigns/{campaignid}/send.{xml|json}
public function scheduleCampaign(string $campaign_id, string $date_time, string $confirmation_emails) {
$result = $this->makeCall('post','campaigns/'.$campaign_id.'/send',[
public function scheduleCampaign(string $campaign_id, string $date_time, string $confirmation_emails)
{
$result = $this->makeCall('campaigns/' . $campaign_id . '/send', [
'json' => [
'ConfirmationEmail' => $confirmation_emails,
'SendDate' => $date_time
]
]);
if($result->get('code') != '200'){
], 'post');
if ($result->get('code') != '200') {
throw new Exception($result->get('body'));
}
return true;
}

//https://api.createsend.com/api/v3.2/campaigns/{campaignid}/unschedule.{xml|json}
public function unScheduleCampaign(string $campaign_id) {
$result = $this->makeCall('post','campaigns/'.$campaign_id.'/unschedule',[]);
if($result->get('code') != '200'){
public function unScheduleCampaign(string $campaign_id)
{
$result = $this->makeCall('campaigns/' . $campaign_id . '/unschedule', [], 'post');
if ($result->get('code') != '200') {
throw new Exception($result->get('body'));
}
return true;
}

}
}
51 changes: 31 additions & 20 deletions src/LaravelCM/Contracts/BaseClientContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,42 @@

namespace Flobbos\LaravelCM\Contracts;

interface BaseClientContract{

interface BaseClientContract
{

/**
* Set current options for client
*
* @param array $options
* @return \Flobbos\LaravelCM\BaseClient
*/
public function setOptions(array $options): \Flobbos\LaravelCM\BaseClient;


/**
* Get current options for client
*
* @return array
*/
public function getOptions(): array;

/**
* Set the client API Key
* @param string $key
*/
public function setClientApiKey(string $key = null): \Flobbos\LaravelCM\BaseClient;

/**
* Get the current client API Key
*/
public function getClientApiKey(): string;

/**
* Set the client ID
* @param string $client_id
* @return \self
*/
public function setClientID(string $client_id = null): \Flobbos\LaravelCM\BaseClient;

/**
* Get the current client ID
* @return string
Expand All @@ -37,67 +49,66 @@ public function getClientID(): string;
* @param string $list_id
*/
public function setListID(string $list_id = null): \Flobbos\LaravelCM\BaseClient;

/**
* Get the current List ID
*/
public function getListID(): string;

/**
* Set the format of the call
* @param string $format json|xml
*/
public function setFormat(string $format): \Flobbos\LaravelCM\BaseClient;

/**
* Get the current format in use
*/
public function getFormat(): string;

/**
* Get a clean guzzle client object
*/
public function getGuzzle(): \GuzzleHttp\Client;

/**
* Initialize guzzle with a base_uri called by the constructor
* @param type $base_uri
*/
public function initGuzzle($base_uri = null): \Flobbos\LaravelCM\BaseClient;

/**
* Retrieve basic Guzzle options for making an API call
* @return array
*/
public function getAuthInformation(): array;

/**
* Retrieve basic Guzzle options for making an API call
* @return array
*/
public function getBaseRequestData(): array;

/**
* Merge the request data with parameters provided by calling function
* @param array $request_data
* @return type
*/
public function mergeRequestData(array $request_data): array;

/**
* Call the guzzle Client and provide optional validation key to skip
* when validating options
* @param type $skip_key
* @return Guzzle\Client
*/
public function callApi(): \GuzzleHttp\Client;

/**
* Make API call to retrieve data
* @param type $method
* @param type $url
* @param array $request_data
*/
public function makeCall($method = 'get', $url, array $request_data);

}
public function makeCall($url, array $request_data, $method = 'get');
}
Loading

0 comments on commit 35d9c8f

Please sign in to comment.