Completed
Push — master ( f2347c...1e5bea )
by Thijs
19s queued 11s
created

ManagesEvents::eventNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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