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

Commit 5471350

Browse files
Initial commit
0 parents  commit 5471350

File tree

6 files changed

+227
-0
lines changed

6 files changed

+227
-0
lines changed

composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "barnebys/mautic-api-helper",
3+
"license": "MIT",
4+
"type": "library",
5+
"description": "",
6+
"autoload": {
7+
"psr-4": {
8+
"BarnebysMautic\\": "src/"
9+
}
10+
},
11+
"require": {
12+
"php": ">=5.6",
13+
"ext-curl": "*",
14+
"psr/log": "~1.0",
15+
"mautic/api-library": "2.10.0"
16+
}
17+
}

src/Api.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace BarnebysMautic;
4+
5+
use BarnebysMautic\Exception\ContactNotFoundException;
6+
use Mautic\Api\Api as BaseApi;
7+
8+
class Api
9+
{
10+
11+
private static $url;
12+
13+
public static function setBaseUrl($url)
14+
{
15+
self::$url = $url;
16+
}
17+
18+
/**
19+
* @return BaseApi
20+
*
21+
* @throws Exception\AuthInstanceException
22+
*/
23+
private static function getApiInstance()
24+
{
25+
$auth = Auth::getInstance();
26+
27+
return new BaseApi($auth->getMauticAuth(), self::$url);
28+
}
29+
30+
/**
31+
* @param string $mail
32+
* @return integer
33+
*
34+
* @throws ContactNotFoundException
35+
* @throws Exception\AuthInstanceException
36+
* @throws \Exception
37+
*/
38+
public static function getContactIdByMail($mail)
39+
{
40+
$api = self::getApiInstance();
41+
42+
$parameters = [
43+
'search' => $mail,
44+
'limit' => 1
45+
];
46+
47+
$data = $api->makeRequest('contacts', $parameters);
48+
$contacts = $data['contacts'];
49+
if(sizeof($contacts) > 0){
50+
return (int) current($contacts)['id'];
51+
}
52+
53+
throw new ContactNotFoundException();
54+
}
55+
56+
/**
57+
* @param string $email
58+
* @param integer $templateId
59+
* @param array $tokens
60+
* @param boolean $html
61+
*
62+
* @return array
63+
*
64+
* @throws ContactNotFoundException
65+
* @throws Exception\AuthInstanceException
66+
* @throws \Exception
67+
*/
68+
public static function sendToLead($email, $templateId, array $tokens = [], $html = false)
69+
{
70+
return self::sendToContact($email, $templateId, $tokens, $html);
71+
}
72+
73+
/**
74+
* @param string $email
75+
* @param integer $templateId
76+
* @param array $tokens
77+
* @param boolean $html
78+
*
79+
* @return array
80+
*
81+
* @throws ContactNotFoundException
82+
* @throws Exception\AuthInstanceException
83+
* @throws \Exception
84+
*/
85+
public static function sendToContact($email, $templateId, array $tokens = [], $html = false)
86+
{
87+
$contactId = self::getContactIdByMail($email);
88+
89+
$api = self::getApiInstance();
90+
91+
$baseUrl = sprintf('contacts/%s/contact/%s/send', $templateId, $contactId);
92+
$htmlUrl = sprintf('contacts/%s/contact/%s/send-content', $templateId, $contactId);
93+
94+
return $api->makeRequest($html === true ? $htmlUrl : $baseUrl, $tokens, 'POST');
95+
}
96+
97+
}

src/Auth.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace BarnebysMautic;
4+
5+
use BarnebysMautic\Exception\AuthInstanceException;
6+
use Mautic\Auth\ApiAuth;
7+
use Mautic\Auth\AuthInterface;
8+
9+
class Auth
10+
{
11+
12+
/**
13+
* @var self|null
14+
*/
15+
private static $instance;
16+
17+
/**
18+
* @return self
19+
*
20+
* @throws AuthInstanceException
21+
*/
22+
public static function getInstance()
23+
{
24+
if (self::$instance == null)
25+
{
26+
throw new AuthInstanceException();
27+
}
28+
29+
return self::$instance;
30+
}
31+
32+
/**
33+
* @param string $username
34+
* @param string $password
35+
*/
36+
public static function initializeHttpBasic($username, $password)
37+
{
38+
$auth = (new ApiAuth())->newAuth([
39+
'userName' => $username,
40+
'password' => $password
41+
], 'BasicAuth');
42+
43+
44+
self::$instance = new self();
45+
self::$instance->setMauticAuth($auth);
46+
}
47+
48+
/**
49+
* @param string $baseUrl
50+
* @param string $clientKey
51+
* @param string $clientSecret
52+
* @param string $callback
53+
*/
54+
public static function initializeOAuth2($baseUrl, $clientKey, $clientSecret, $callback)
55+
{
56+
$auth = (new ApiAuth())->newAuth([
57+
'baseUrl' => $baseUrl,
58+
'version' => 'OAuth2',
59+
'clientKey' => $clientKey,
60+
'clientSecret' => $clientSecret,
61+
'callback' => $callback
62+
], 'OAuth');
63+
64+
self::$instance = new self();
65+
self::$instance->setMauticAuth($auth);
66+
}
67+
68+
/**
69+
* @var AuthInterface
70+
*/
71+
private $mauticAuthInstance;
72+
73+
/**
74+
* @param AuthInterface $auth
75+
*/
76+
public function setMauticAuth(AuthInterface $auth)
77+
{
78+
$this->mauticAuthInstance = $auth;
79+
}
80+
81+
/**
82+
* @return AuthInterface
83+
*/
84+
public function getMauticAuth()
85+
{
86+
return $this->mauticAuthInstance;
87+
}
88+
89+
}
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 AuthInstanceException 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 ContactNotFoundException 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 MauticApiException extends \Exception
6+
{
7+
8+
}

0 commit comments

Comments
 (0)