<?php
namespace App\Service;
use TestMonitor\ActiveCampaign\ActiveCampaign;
use TestMonitor\ActiveCampaign\MakesHttpRequests;
use TestMonitor\ActiveCampaign\Resources\Contact;
use TestMonitor\ActiveCampaign\Resources\Tag;
use TestMonitor\ActiveCampaign\Resources\ContactsList;
class ActiveCampaignApi extends ActiveCampaign
{
use MakesHttpRequests;
public function __construct($apiUrl,$apiKey)
{
parent::__construct($apiUrl, $apiKey);
}
public function getTotalResults($type){
$res = $this->get($type);
$total = 0 ;
if(array_key_exists('meta',$res)){
if(array_key_exists('total',$res['meta'])){
$total = $res['meta']['total'];
}
}
return $total ;
}
public function findTagList($name)
{
$tags = [];
if($name != null and trim($name) != ""){
$searchtags = $this->get('tags', ['query' => ['search' => $name]]);
if(array_key_exists('tags', $searchtags)){
foreach ($searchtags['tags'] as $t) {
if(strtolower($t['tag']) == strtolower($name)){
$tags = $this->transformCollection(
[$t] ,
Tag::class,
'tags'
);
}
}
}
// Create Tags if not exist
if ($tags == null) {
$tags = $this->createTag(['tag' => $name, 'tagType' => 'contact' ]);
}else{
$tags = array_shift($tags) ;
}
}
return $tags;
}
/**
* Finds list by it's name or URL-safe name.
*
* @param string $name name of list to find
*
* @return null|ContactsList
*/
public function findListByLimit($type = "", $limit , $offset = 0)
{
$numberResult = $this->getTotalResults($type);
$nbPages = ceil($numberResult / $limit) ;
$res = [];
foreach (range(0, ($nbPages - 1 )) as $number) {
$offset = ( $number * $limit ) ;
$class = "";
if($type == "lists") $class = ContactsList::class ;
if($type == "tags") $class = Tag::class ;
$lists = $this->transformCollection(
$this->get($type, [ 'query' => ['limit' => $limit, 'offset' => $offset] ] ),
$class ,
$type
);
$res = array_merge($res, $lists);
}
return ($res);
}
}