Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit c42680f

Browse files
Adds support for Tags
1 parent 05b6197 commit c42680f

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

src/Api.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace BarnebysMautic;
44

5+
use BarnebysMautic\Exception\ContactCreateException;
6+
use BarnebysMautic\Tags;
57
use BarnebysMautic\Exception\ContactNotFoundException;
8+
use BarnebysMautic\Exception\SegmentNotFoundException;
69
use Mautic\Api\Api as BaseApi;
710

811
class Api
@@ -106,7 +109,7 @@ public static function sendToContact($email, $templateId, array $tokens = [], $h
106109
* @throws Exception\AuthInstanceException
107110
* @throws \Exception
108111
*/
109-
public static function updateContact($email, $data = []) {
112+
public static function updateContact($email, array $data) {
110113

111114
$contactId = self::getContactIdByMail($email);
112115

@@ -120,10 +123,11 @@ public static function updateContact($email, $data = []) {
120123
/**
121124
* @param $email
122125
* @param array $data
123-
* @return mixed
126+
* @return array
124127
* @throws Exception\AuthInstanceException
128+
* @throws \Exception
125129
*/
126-
public static function createContact($email, $data = []) {
130+
public static function createContact($email, array $data = []) {
127131

128132
$api = self::getApiInstance();
129133

@@ -132,7 +136,14 @@ public static function createContact($email, $data = []) {
132136
'ipAddress' => $_SERVER['REMOTE_ADDR']
133137
], $data);
134138

135-
return $api->makeRequest('contacts/new', $data, 'POST');
139+
140+
$data = $api->makeRequest('contacts/new', $data, 'POST');
141+
142+
if (isset($data['contact']) && isset($data['contact']['id'])) {
143+
return $data['contact']['id'];
144+
}
145+
146+
throw new ContactCreateException();
136147
}
137148

138149
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace BarnebysMautic\Exception;
4+
5+
class ContactCreateException extends MauticApiException
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace BarnebysMautic\Exception;
4+
5+
class SegmentNotFoundException extends MauticApiException
6+
{
7+
8+
}

src/Tags.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace BarnebysMautic;
4+
5+
class Tags
6+
{
7+
8+
private $_tags = [];
9+
10+
/**
11+
* @param $name
12+
* @return $this
13+
*/
14+
public function addTag($name) {
15+
if (!in_array($name, $this->_tags)) {
16+
$this->_tags[] = $name;
17+
}
18+
19+
return $this;
20+
}
21+
22+
/**
23+
* @param $name
24+
* @return $this
25+
*/
26+
public function removeTag($name) {
27+
if (in_array($name, $this->_tags)) {
28+
unset($this->_tags[$name]);
29+
}
30+
31+
$this->_tags[] = '-' . $name;
32+
33+
return $this;
34+
}
35+
36+
/**
37+
* @return array
38+
*/
39+
public function toArray()
40+
{
41+
return ['tags' => $this->_tags];
42+
}
43+
44+
}

0 commit comments

Comments
 (0)