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

ManagesEvents::eventNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace TestMonitor\ActiveCampaign\Actions;
4
5
use TestMonitor\ActiveCampaign\Resources\Event;
6
7
trait ManagesEvents
8
{
9
    /**
10
     * This value is unique to ActiveCampaign account and can be found named "Event Key"
11
     * on Settings > Tracking > Event Tracking inside ActiveCampaign account.
12
     *
13
     * @var string
14
     */
15
    public $eventKey;
16
17
    /**
18
     * This value is unique to ActiveCampaign account and can be found named "actid"
19
     * on Settings > Tracking > Event Tracking API.
20
     *
21
     * @var string
22
     */
23
    public $actid;
24
25
    /**
26
     * Creates a new event (name only)
27
     *
28
     * @param string $name
29
     *
30
     * @return Event
31
     */
32
    public function createEvent($name)
33
    {
34
        $data = [
35
            'name' => $name,
36
        ];
37
38
        $event = $this->post('eventTrackingEvents', ['json' => ['eventTrackingEvent' => $data]]);
0 ignored issues
show
Bug introduced by
It seems like post() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39
40
        return new Event($event['eventTrackingEvent']);
41
    }
42
43
    /**
44
     * List all events
45
     *
46
     * @return Event[]
47
     */
48
    public function events()
49
    {
50
        return $this->transformCollection(
0 ignored issues
show
Bug introduced by
It seems like transformCollection() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
51
            $this->get('eventTrackingEvents'),
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
52
            Event::class,
53
            'eventTrackingEvents'
54
        );
55
    }
56
57
    /**
58
     * List name of all events
59
     *
60
     * @return Event[]
61
     */
62
    public function eventNames()
63
    {
64
        $events = $this->get('eventTrackingEvents');
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
65
66
        return array_map(function ($data) { return $data['name']; },
67
            $events['eventTrackingEvents'] ?? $events);
68
    }
69
70
    /**
71
     * Removes an existing event tracking event (name only)
72
     *
73
     * @param $name
74
     *
75
     * @throws \TestMonitor\ActiveCampaign\Exceptions\NotFoundException
76
     */
77
    public function deleteEvent($name)
78
    {
79
        $this->delete('eventTrackingEvents/' . $name);
0 ignored issues
show
Bug introduced by
The method delete() does not exist on TestMonitor\ActiveCampaign\Actions\ManagesEvents. Did you maybe mean deleteEvent()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
80
    }
81
82
    /**
83
     * Tracks an event by name
84
     *
85
     * @param string $name      name of the event to track
86
     * @param string $email     email address of the contact to track this event for, optional
87
     * @param array  $eventData a value to store for the event, optional
88
     *
89
     * @return bool TRUE on success, FALSE otherwise
90
     * @throws \TestMonitor\ActiveCampaign\Exceptions\FailedActionException
91
     */
92
    public function trackEvent($name, $email = '', $eventData = [])
93
    {
94
        $curl = curl_init();
95
        curl_setopt($curl, CURLOPT_URL, 'https://trackcmp.net/event');
96
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
97
        curl_setopt($curl, CURLOPT_POST, true);
98
        curl_setopt($curl, CURLOPT_POSTFIELDS, [
99
            'actid'     => $this->actid,
100
            'key'       => $this->eventKey,
101
            'event'     => $name,
102
            'eventdata' => json_encode($eventData),
103
        ] + ($email ? ['visit' => json_encode(['email' => $email])] : []));
104
105
        $result = curl_exec($curl);
106
        if ($result !== false)
107
        {
108
            curl_close($curl);
109
            $result = json_decode($result);
110
111
            if (isset($result->success) && $result->success) return true;
112
113
            return false;
114
        }
115
        else
116
        {
117
            throw new \TestMonitor\ActiveCampaign\Exceptions\FailedActionException(curl_error($curl));
118
        }
119
    }
120
}