Skip to content

Commit 703ed57

Browse files
authored
Merge pull request #42 from mailjet/contacts-v4-endpoint
allowed contacts removing
2 parents 0f2a1fa + 12975bb commit 703ed57

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,23 @@ MAIL_FROM_NAME=YOU_FROM_NAME
8181
'call' => true,
8282
'secured' => true
8383
]
84-
]
84+
],
85+
'v4' => [
86+
'call' => true,
87+
'options' => [
88+
'url' => 'api.mailjet.com',
89+
'version' => 'v4',
90+
'call' => true,
91+
'secured' => true
92+
]
93+
],
8594
]
8695
```
8796
You can pass settings to [MailjetClient](https://github.com/mailjet/mailjet-apiv3-php#new--version-120-of-the-php-wrapper-).
8897

8998
* `transactional`: settings to sendAPI client
9099
* `common`: setting to MailjetClient accessible throught the Facade Mailjet
100+
* `v4`: setting used for some DataProvider`s
91101

92102
## Mail driver configuration
93103

@@ -137,6 +147,26 @@ All method return `Mailjet\Response` or throw a `MailjetException` in case of AP
137147

138148
You can also get the Mailjet API client with the method `getClient()` and make your own custom request to Mailjet API.
139149

150+
If you need to delete a contact, you need to register ContactsServiceProvider:
151+
* In the providers array:
152+
153+
```php
154+
'providers' => [
155+
...
156+
\Mailjet\LaravelMailjet\Providers\ContactsServiceProvider::class,
157+
...
158+
]
159+
```
160+
161+
and use it:
162+
```php
163+
public function handle(ContactsV4Service $contactsV4Service)
164+
{
165+
$response = $contactsV4Service->delete(351406781);
166+
...
167+
}
168+
```
169+
140170
## ToDo
141171

142172
* Add additional unit tests to increase code coverage.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/*
3+
* To change this license header, choose License Headers in Project Properties.
4+
* To change this template file, choose Tools | Templates
5+
* and open the template in the editor.
6+
*/
7+
8+
namespace Mailjet\LaravelMailjet\Contracts;
9+
10+
interface ContactsV4Contract
11+
{
12+
public function delete($id);
13+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Mailjet\LaravelMailjet\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Mailjet\LaravelMailjet\Contracts\ContactsV4Contract;
7+
use Mailjet\LaravelMailjet\Services\ContactsV4Service;
8+
use Mailjet\LaravelMailjet\Services\MailjetService;
9+
10+
class ContactsServiceProvider extends ServiceProvider
11+
{
12+
protected $defer = true;
13+
14+
/**
15+
* Bootstrap the application services.
16+
*
17+
* @return void
18+
*/
19+
public function boot()
20+
{
21+
22+
}
23+
24+
/**
25+
* Register the application services.
26+
*
27+
* @return void
28+
*/
29+
public function register()
30+
{
31+
$this->app->bind(ContactsV4Service::class, function($app) {
32+
$config = $this->app['config']->get('services.mailjet', []);
33+
$call = $this->app['config']->get('services.mailjet.v4.call', true);
34+
$options = $this->app['config']->get('services.mailjet.v4.options', []);
35+
36+
$mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options);
37+
38+
return new ContactsV4Service($mailjetService);
39+
});
40+
}
41+
42+
/**
43+
* Get the services provided by the provider.
44+
*
45+
* @return array
46+
*/
47+
public function provides()
48+
{
49+
return [ContactsV4Service::class];
50+
}
51+
}

src/Services/ContactsV4Service.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Mailjet\LaravelMailjet\Services;
4+
5+
use Mailjet\LaravelMailjet\Contracts\ContactsV4Contract;
6+
use \Mailjet\Resources;
7+
use Mailjet\LaravelMailjet\Exception\MailjetException;
8+
9+
/**
10+
* https://dev.mailjet.com/email/guides/contact-management/#gdpr-delete-contacts
11+
*/
12+
class ContactsV4Service implements ContactsV4Contract
13+
{
14+
/**
15+
* Mailjet client
16+
* @var MailjetService
17+
*/
18+
protected $mailjet;
19+
20+
/**
21+
* @param MailjetService $mailjet
22+
*/
23+
public function __construct(MailjetService $mailjet)
24+
{
25+
$this->mailjet = $mailjet;
26+
}
27+
28+
/**
29+
* Delete a Contact
30+
* @param int $id
31+
* @return bool
32+
*/
33+
public function delete($id)
34+
{
35+
$response = $this->mailjet->delete(['contacts', ''], ['id' => $id]);
36+
37+
if (!$response->success()) {
38+
throw new MailjetException(0, "ContactsV4Service:delete() failed", $response);
39+
}
40+
41+
return 200 === $response->getStatus();
42+
}
43+
}

0 commit comments

Comments
 (0)