ActiveCampaign::transformCollection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
namespace TestMonitor\ActiveCampaign;
4
5
use GuzzleHttp\Client as HttpClient;
6
use TestMonitor\ActiveCampaign\Actions\ManagesTags;
7
use TestMonitor\ActiveCampaign\Actions\ManagesLists;
8
use TestMonitor\ActiveCampaign\Actions\ManagesEvents;
9
use TestMonitor\ActiveCampaign\Actions\ManagesAccounts;
10
use TestMonitor\ActiveCampaign\Actions\ManagesContacts;
11
use TestMonitor\ActiveCampaign\Actions\ManagesAutomations;
12
use TestMonitor\ActiveCampaign\Actions\ManagesContactTags;
13
use TestMonitor\ActiveCampaign\Actions\ManagesCustomFields;
14
use TestMonitor\ActiveCampaign\Actions\ManagesOrganizations;
15
use TestMonitor\ActiveCampaign\Actions\ManagesAccountContacts;
16
use TestMonitor\ActiveCampaign\Actions\ManagesContactAutomations;
17
use TestMonitor\ActiveCampaign\Actions\ManagesAccountCustomFields;
18
19
class ActiveCampaign
20
{
21
    use MakesHttpRequests,
22
        ManagesAccounts,
23
        ManagesAccountContacts,
24
        ManagesAccountCustomFields,
25
        ManagesAutomations,
26
        ManagesContacts,
27
        ManagesTags,
28
        ManagesContactTags,
29
        ManagesContactAutomations,
30
        ManagesCustomFields,
31
        ManagesOrganizations,
0 ignored issues
show
Deprecated Code introduced by
The trait TestMonitor\ActiveCampai...ns\ManagesOrganizations has been deprecated with message: use ManageAccounts instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
32
        ManagesEvents,
33
        ManagesLists;
34
35
    /**
36
     * The ActiveCampaign base url.
37
     *
38
     * @var string
39
     */
40
    public $apiUrl;
41
42
    /**
43
     * The ActiveCampaign API token.
44
     *
45
     * @var string
46
     */
47
    public $apiKey;
48
49
    /**
50
     * The Guzzle HTTP Client instance.
51
     *
52
     * @var \GuzzleHttp\Client
53
     */
54
    public $guzzle;
55
56
    /**
57
     * Create a new ActiveCampaign instance.
58
     *
59
     * @param string $apiUrl
60
     * @param string $apiKey
61
     * @param \GuzzleHttp\Client $guzzle
62
     */
63
    public function __construct($apiUrl, $apiKey, HttpClient $guzzle = null)
64
    {
65
        $this->apiUrl = $apiUrl;
66
        $this->apiKey = $apiKey;
67
68
        $this->guzzle = $guzzle ?: new HttpClient([
69
            'base_uri' => "{$this->apiUrl}/api/3/",
70
            'http_errors' => false,
71
            'headers' => [
72
                'Content-Type' => 'application/json',
73
                'Accept' => 'application/json',
74
                'Api-Token' => $this->apiKey,
75
            ],
76
        ]);
77
    }
78
79
    /**
80
     * Transform the items of the collection to the given class.
81
     *
82
     * @param  array $collection
83
     * @param  string $class
84
     * @param  string $key
85
     * @param  array $extraData
86
     *
87
     * @return array
88
     */
89
    protected function transformCollection($collection, $class, $key = '', $extraData = [])
90
    {
91
        return array_map(function ($data) use ($class, $extraData) {
92
            return new $class($data + $extraData, $this);
93
        }, $collection[$key] ?? $collection);
94
    }
95
}
96