vendor/testmonitor/activecampaign/src/ActiveCampaign.php line 68

Open in your IDE?
  1. <?php
  2. namespace TestMonitor\ActiveCampaign;
  3. use GuzzleHttp\Client as HttpClient;
  4. use TestMonitor\ActiveCampaign\Actions\ManagesTags;
  5. use TestMonitor\ActiveCampaign\Actions\ManagesLists;
  6. use TestMonitor\ActiveCampaign\Actions\ManagesEvents;
  7. use TestMonitor\ActiveCampaign\Actions\ManagesAccounts;
  8. use TestMonitor\ActiveCampaign\Actions\ManagesContacts;
  9. use TestMonitor\ActiveCampaign\Actions\ManagesAutomations;
  10. use TestMonitor\ActiveCampaign\Actions\ManagesContactTags;
  11. use TestMonitor\ActiveCampaign\Actions\ManagesCustomFields;
  12. use TestMonitor\ActiveCampaign\Actions\ManagesOrganizations;
  13. use TestMonitor\ActiveCampaign\Actions\ManagesAccountContacts;
  14. use TestMonitor\ActiveCampaign\Actions\ManagesContactAutomations;
  15. use TestMonitor\ActiveCampaign\Actions\ManagesAccountCustomFields;
  16. class ActiveCampaign
  17. {
  18. use MakesHttpRequests,
  19. ManagesAccounts,
  20. ManagesAccountContacts,
  21. ManagesAccountCustomFields,
  22. ManagesAutomations,
  23. ManagesContacts,
  24. ManagesTags,
  25. ManagesContactTags,
  26. ManagesContactAutomations,
  27. ManagesCustomFields,
  28. ManagesOrganizations,
  29. ManagesEvents,
  30. ManagesLists;
  31. /**
  32. * The ActiveCampaign base url.
  33. *
  34. * @var string
  35. */
  36. public $apiUrl;
  37. /**
  38. * The ActiveCampaign API token.
  39. *
  40. * @var string
  41. */
  42. public $apiKey;
  43. /**
  44. * The Guzzle HTTP Client instance.
  45. *
  46. * @var \GuzzleHttp\Client
  47. */
  48. public $guzzle;
  49. /**
  50. * Create a new ActiveCampaign instance.
  51. *
  52. * @param string $apiUrl
  53. * @param string $apiKey
  54. * @param \GuzzleHttp\Client $guzzle
  55. */
  56. public function __construct($apiUrl, $apiKey, HttpClient $guzzle = null)
  57. {
  58. $this->apiUrl = $apiUrl;
  59. $this->apiKey = $apiKey;
  60. $this->guzzle = $guzzle ?: new HttpClient([
  61. 'base_uri' => "{$this->apiUrl}/api/3/",
  62. 'http_errors' => false,
  63. 'headers' => [
  64. 'Content-Type' => 'application/json',
  65. 'Accept' => 'application/json',
  66. 'Api-Token' => $this->apiKey,
  67. ],
  68. ]);
  69. }
  70. /**
  71. * Transform the items of the collection to the given class.
  72. *
  73. * @param array $collection
  74. * @param string $class
  75. * @param string $key
  76. * @param array $extraData
  77. *
  78. * @return array
  79. */
  80. protected function transformCollection($collection, $class, $key = '', $extraData = [])
  81. {
  82. return array_map(function ($data) use ($class, $extraData) {
  83. return new $class($data + $extraData, $this);
  84. }, $collection[$key] ?? $collection);
  85. }
  86. }