src/Service/ActiveCampaignApi.php line 84

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 = 0 ;
  18. if(array_key_exists('meta',$res)){
  19. if(array_key_exists('total',$res['meta'])){
  20. $total = $res['meta']['total'];
  21. }
  22. }
  23. return $total ;
  24. }
  25. public function findTagList($name)
  26. {
  27. $tags = [];
  28. if($name != null and trim($name) != ""){
  29. $searchtags = $this->get('tags', ['query' => ['search' => $name]]);
  30. if(array_key_exists('tags', $searchtags)){
  31. foreach ($searchtags['tags'] as $t) {
  32. if(strtolower($t['tag']) == strtolower($name)){
  33. $tags = $this->transformCollection(
  34. [$t] ,
  35. Tag::class,
  36. 'tags'
  37. );
  38. }
  39. }
  40. }
  41. // Create Tags if not exist
  42. if ($tags == null) {
  43. $tags = $this->createTag(['tag' => $name, 'tagType' => 'contact' ]);
  44. }else{
  45. $tags = array_shift($tags) ;
  46. }
  47. }
  48. return $tags;
  49. }
  50. /**
  51. * Finds list by it's name or URL-safe name.
  52. *
  53. * @param string $name name of list to find
  54. *
  55. * @return null|ContactsList
  56. */
  57. public function findListByLimit($type = "", $limit , $offset = 0)
  58. {
  59. $numberResult = $this->getTotalResults($type);
  60. $nbPages = ceil($numberResult / $limit) ;
  61. $res = [];
  62. foreach (range(0, ($nbPages - 1 )) as $number) {
  63. $offset = ( $number * $limit ) ;
  64. $class = "";
  65. if($type == "lists") $class = ContactsList::class ;
  66. if($type == "tags") $class = Tag::class ;
  67. $lists = $this->transformCollection(
  68. $this->get($type, [ 'query' => ['limit' => $limit, 'offset' => $offset] ] ),
  69. $class ,
  70. $type
  71. );
  72. $res = array_merge($res, $lists);
  73. }
  74. return ($res);
  75. }
  76. }