Completed
Pull Request — master (#18)
by
unknown
01:06
created

ManagesEvents   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 3
dl 0
loc 117
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createEvent() 0 10 1
A events() 0 8 1
A eventNames() 0 9 1
A deleteEvent() 0 4 1
A trackEvent() 0 27 5
1
<?php
2
3
namespace TestMonitor\ActiveCampaign\Actions;
4
5
use TestMonitor\ActiveCampaign\Actions\Action;
6
use TestMonitor\ActiveCampaign\Resources\Event;
7
8
trait ManagesEvents
9
{
10
    use Action;
11
12
    /**
13
     * This value is unique to ActiveCampaign account and can be found named "Event Key"
14
     * on Settings > Tracking > Event Tracking inside ActiveCampaign account.
15
     *
16
     * @var string
17
     */
18
    public $eventKey;
19
20
    /**
21
     * This value is unique to ActiveCampaign account and can be found named "actid"
22
     * on Settings > Tracking > Event Tracking API.
23
     *
24
     * @var string
25
     */
26
    public $actid;
27
28
    /**
29
     * Creates a new event (name only).
30
     *
31
     * @param string $name
32
     *
33
     * @return Event
34
     */
35
    public function createEvent($name)
36
    {
37
        $data = [
38
            'name' => $name,
39
        ];
40
41
        $event = $this->post('eventTrackingEvents', ['json' => ['eventTrackingEvent' => $data]]);
42
43
        return new Event($event['eventTrackingEvent']);
44
    }
45
46
    /**
47
     * List all events.
48
     *
49
     * @return Event[]
50
     */
51
    public function events()
52
    {
53
        return $this->transformCollection(
54
            $this->get('eventTrackingEvents'),
55
            Event::class,
56
            'eventTrackingEvents'
57
        );
58
    }
59
60
    /**
61
     * List name of all events.
62
     *
63
     * @return Event[]
64
     */
65
    public function eventNames()
66
    {
67
        $events = $this->get('eventTrackingEvents');
68
69
        return array_map(function ($data) {
70
            return $data['name'];
71
        },
72
            $events['eventTrackingEvents'] ?? $events);
73
    }
74
75
    /**
76
     * Removes an existing event tracking event (name only).
77
     *
78
     * @param $name
79
     *
80
     * @throws \TestMonitor\ActiveCampaign\Exceptions\NotFoundException
81
     */
82
    public function deleteEvent($name)
83
    {
84
        $this->delete('eventTrackingEvents/'.$name);
85
    }
86
87
    /**
88
     * Tracks an event by name.
89
     *
90
     * @param string $name      name of the event to track
91
     * @param string $email     email address of the contact to track this event for, optional
92
     * @param array  $eventData a value to store for the event, optional
93
     *
94
     * @return bool TRUE on success, FALSE otherwise
95
     * @throws \TestMonitor\ActiveCampaign\Exceptions\FailedActionException
96
     */
97
    public function trackEvent($name, $email = '', $eventData = [])
98
    {
99
        $curl = curl_init();
100
        curl_setopt($curl, CURLOPT_URL, 'https://trackcmp.net/event');
101
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
102
        curl_setopt($curl, CURLOPT_POST, true);
103
        curl_setopt($curl, CURLOPT_POSTFIELDS, [
104
            'actid'     => $this->actid,
105
            'key'       => $this->eventKey,
106
            'event'     => $name,
107
            'eventdata' => json_encode($eventData),
108
        ] + ($email ? ['visit' => json_encode(['email' => $email])] : []));
109
110
        $result = curl_exec($curl);
111
        if ($result !== false) {
112
            curl_close($curl);
113
            $result = json_decode($result);
114
115
            if (isset($result->success) && $result->success) {
116
                return true;
117
            }
118
119
            return false;
120
        } else {
121
            throw new \TestMonitor\ActiveCampaign\Exceptions\FailedActionException(curl_error($curl));
122
        }
123
    }
124
}
125