Skip to content

Commit

Permalink
Merge pull request #42 from mailjet/contacts-v4-endpoint
Browse files Browse the repository at this point in the history
allowed contacts removing
  • Loading branch information
uavn authored Dec 7, 2020
2 parents 0f2a1fa + 12975bb commit 703ed57
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 1 deletion.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,23 @@ MAIL_FROM_NAME=YOU_FROM_NAME
'call' => true,
'secured' => true
]
]
],
'v4' => [
'call' => true,
'options' => [
'url' => 'api.mailjet.com',
'version' => 'v4',
'call' => true,
'secured' => true
]
],
]
```
You can pass settings to [MailjetClient](https://github.com/mailjet/mailjet-apiv3-php#new--version-120-of-the-php-wrapper-).

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

## Mail driver configuration

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

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

If you need to delete a contact, you need to register ContactsServiceProvider:
* In the providers array:

```php
'providers' => [
...
\Mailjet\LaravelMailjet\Providers\ContactsServiceProvider::class,
...
]
```

and use it:
```php
public function handle(ContactsV4Service $contactsV4Service)
{
$response = $contactsV4Service->delete(351406781);
...
}
```

## ToDo

* Add additional unit tests to increase code coverage.
13 changes: 13 additions & 0 deletions src/Contracts/ContactsV4Contract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

namespace Mailjet\LaravelMailjet\Contracts;

interface ContactsV4Contract
{
public function delete($id);
}
51 changes: 51 additions & 0 deletions src/Providers/ContactsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Mailjet\LaravelMailjet\Providers;

use Illuminate\Support\ServiceProvider;
use Mailjet\LaravelMailjet\Contracts\ContactsV4Contract;
use Mailjet\LaravelMailjet\Services\ContactsV4Service;
use Mailjet\LaravelMailjet\Services\MailjetService;

class ContactsServiceProvider extends ServiceProvider
{
protected $defer = true;

/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{

}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind(ContactsV4Service::class, function($app) {
$config = $this->app['config']->get('services.mailjet', []);
$call = $this->app['config']->get('services.mailjet.v4.call', true);
$options = $this->app['config']->get('services.mailjet.v4.options', []);

$mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options);

return new ContactsV4Service($mailjetService);
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [ContactsV4Service::class];
}
}
43 changes: 43 additions & 0 deletions src/Services/ContactsV4Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Mailjet\LaravelMailjet\Services;

use Mailjet\LaravelMailjet\Contracts\ContactsV4Contract;
use \Mailjet\Resources;
use Mailjet\LaravelMailjet\Exception\MailjetException;

/**
* https://dev.mailjet.com/email/guides/contact-management/#gdpr-delete-contacts
*/
class ContactsV4Service implements ContactsV4Contract
{
/**
* Mailjet client
* @var MailjetService
*/
protected $mailjet;

/**
* @param MailjetService $mailjet
*/
public function __construct(MailjetService $mailjet)
{
$this->mailjet = $mailjet;
}

/**
* Delete a Contact
* @param int $id
* @return bool
*/
public function delete($id)
{
$response = $this->mailjet->delete(['contacts', ''], ['id' => $id]);

if (!$response->success()) {
throw new MailjetException(0, "ContactsV4Service:delete() failed", $response);
}

return 200 === $response->getStatus();
}
}

0 comments on commit 703ed57

Please sign in to comment.