src/Service/ActiveCampaignApi.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use TestMonitor\ActiveCampaign\ActiveCampaign;
  4. use TestMonitor\ActiveCampaign\MakesHttpRequests;
  5. use TestMonitor\ActiveCampaign\Resources\Contact;
  6. use TestMonitor\ActiveCampaign\Resources\Tag;
  7. use TestMonitor\ActiveCampaign\Resources\ContactsList;
  8. class ActiveCampaignApi extends ActiveCampaign
  9. {
  10.     use MakesHttpRequests;
  11.     public function __construct($apiUrl,$apiKey)
  12.     {
  13.         parent::__construct($apiUrl$apiKey);
  14.     }
  15.     public function getTotalResults($type){
  16.         $res $this->get($type);
  17.         $total ;
  18.         if(array_key_exists('meta',$res)){
  19.             if(array_key_exists('total',$res['meta'])){
  20.                 $total $res['meta']['total'];
  21.             }
  22.         }
  23.         
  24.         return $total ;
  25.     }
  26.     public function findTagList($name)
  27.     {
  28.         $tags = [];
  29.         if($name != null and trim($name) != ""){
  30.                 
  31.             $searchtags $this->get('tags', ['query' => ['search' => $name]]);
  32.             
  33.             
  34.             if(array_key_exists('tags'$searchtags)){
  35.                 foreach ($searchtags['tags'] as $t) {
  36.                     if(strtolower($t['tag']) == strtolower($name)){
  37.                         $tags $this->transformCollection(
  38.                             [$t] ,
  39.                             Tag::class,
  40.                             'tags'
  41.                         );
  42.                     }
  43.                 }
  44.             }
  45.             // Create Tags if not exist
  46.             if ($tags == null) {
  47.                 $tags $this->createTag(['tag' => $name'tagType' => 'contact' ]);
  48.             }else{
  49.                 $tags array_shift($tags) ;
  50.             }
  51.         }
  52.         return $tags;
  53.         
  54.     }
  55.     /**
  56.      * Finds list by it's name or URL-safe name.
  57.      *
  58.      * @param string $name name of list to find
  59.      *
  60.      * @return null|ContactsList
  61.      */
  62.     public function findListByLimit($type ""$limit $offset 0)
  63.     {
  64.         
  65.         $numberResult $this->getTotalResults($type);
  66.         $nbPages ceil($numberResult $limit) ;
  67.         $res = [];
  68.         foreach (range(0, ($nbPages )) as $number) {
  69.             $offset = ( $number $limit ) ;
  70.             $class "";
  71.             if($type == "lists"$class ContactsList::class ;
  72.             if($type == "tags"$class Tag::class ;
  73.             $lists $this->transformCollection(
  74.                 $this->get($type, [ 'query' => ['limit' => $limit'offset' => $offset] ] ),
  75.                 $class ,
  76.                 $type
  77.             );
  78.             $res array_merge($res$lists);
  79.         }
  80.         return ($res);
  81.     }
  82. }