Skip to content

Commit

Permalink
Contactlists, Emails and Newsletter endpoints (pionl#7)
Browse files Browse the repository at this point in the history
Add contactlist API
  • Loading branch information
PavelJurasek authored Jul 1, 2020
1 parent 2900ddb commit ba2aa04
Show file tree
Hide file tree
Showing 10 changed files with 385 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"minimum-stability": "stable",
"require": {
"php": ">=5.5",
"php": ">=7.3",
"guzzlehttp/guzzle": "^6.2"
},
"require-dev": {
Expand Down
12 changes: 8 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ API wrapper for [Smart emailing](http://smartemailing.cz) API. Currenlty in deve

## Installation

**Requirements**

This package requires PHP 7.3 and higher.

**Install via composer**

```
Expand Down Expand Up @@ -79,7 +83,7 @@ try {
* [x] [Import](https://app.smartemailing.cz/docs/api/v3/index.html#api-Import-Import_contacts)`$api->import()` or `new Import($api)`
* [x] [Ping](https://app.smartemailing.cz/docs/api/v3/index.html#api-Tests-Aliveness_test) `$api->ping()` or `new Ping($api)`
* [x] [Credentials](https://app.smartemailing.cz/docs/api/v3/index.html#api-Tests-Login_test_with_GET) `$api->credentials()` or `new Credentials($api)`
* [ ] [Contactlist](https://app.smartemailing.cz/docs/api/v3/index.html#api-Contactlists-Get_Contactlists) Retrieve list `$api->contactlist()->lists()` or detail `$api->contactlist()->get($id)` - wrapper for 2 Request objects
* [x] [Contactlist](https://app.smartemailing.cz/docs/api/v3/index.html#api-Contactlists-Get_Contactlists) Retrieve list `$api->contactlist()->lists()` or detail `$api->contactlist()->get($id)` - wrapper for 2 Request objects
* [x] CustomFields - exists: A quick way how to get custom field by it's name. `$api->customFields()->exists('name') : CustomField|bool`
* [x] [Customfields - create](https://app.smartemailing.cz/docs/api/v3/index.html#api-Customfields) create request `$api->customFields()->createRequest()` or send create request `$api->customFields()->create(new CustomField('test', CustomField::TEXT))`
* [x] [Customfields - search / list](https://app.smartemailing.cz/docs/api/v3/index.html#api-Customfields) search request `$api->customFields()->searchRequest($page = 1, $limit = 100)` or send search request `$api->customFields()->search($page = 1, $limit = 100)`
Expand All @@ -88,8 +92,8 @@ try {
* [ ] [Contacts](https://app.smartemailing.cz/docs/api/v3/index.html#api-Contacts) Similar concept as contact-list
* [ ] [Contacts in list](https://app.smartemailing.cz/docs/api/v3/index.html#api-Contacts_in_lists) Similar concept as contact-list
* [ ] [Custom emails](https://app.smartemailing.cz/docs/api/v3/index.html#api-Custom_emails)
* [ ] [Emails](https://app.smartemailing.cz/docs/api/v3/index.html#api-Emails)
* [ ] [Newsletter](https://app.smartemailing.cz/docs/api/v3/index.html#api-Newsletter)
* [x] [Emails](https://app.smartemailing.cz/docs/api/v3/index.html#api-Emails)
* [x] [Newsletter](https://app.smartemailing.cz/docs/api/v3/index.html#api-Newsletter)
* [ ] [Webhooks](https://app.smartemailing.cz/docs/api/v3/index.html#api-Webhooks)

## Advanced docs
Expand Down Expand Up @@ -303,4 +307,4 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for how to contribute changes. All contri
was written by [Martin Kluska](http://kluska.cz) and is released under the
[MIT License](LICENSE.md).

Copyright (c) 2016 Martin Kluska
Copyright (c) 2016 Martin Kluska
30 changes: 29 additions & 1 deletion src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
namespace SmartEmailing\v3;

use GuzzleHttp\Client;
use SmartEmailing\v3\Request\Contactlists\ContactlistEndpoint;
use SmartEmailing\v3\Request\Contactlists\Contactlists;
use SmartEmailing\v3\Request\Credentials\Credentials;
use SmartEmailing\v3\Request\CustomFields\CustomFields;
use SmartEmailing\v3\Request\Email\EmailsEndpoint;
use SmartEmailing\v3\Request\Import\Import;
use SmartEmailing\v3\Request\Newsletter\Newsletter;
use SmartEmailing\v3\Request\Ping\Ping;

/**
Expand Down Expand Up @@ -54,6 +58,30 @@ public function import()
return new Import($this);
}

/**
* Creates new contactlists proxy
*/
public function contactlist(): ContactlistEndpoint
{
return new ContactlistEndpoint($this);
}

/**
* Creates new email proxy
*/
public function email(): EmailsEndpoint
{
return new EmailsEndpoint($this);
}

/**
* Creates new contactlists proxy
*/
public function newsletter(int $emailId, array $contactLists): Newsletter
{
return new Newsletter($this, $emailId, $contactLists);
}

/**
* @return Ping
*/
Expand All @@ -75,4 +103,4 @@ public function customFields()
return new CustomFields($this);
}

}
}
11 changes: 10 additions & 1 deletion src/Exceptions/InvalidFormatException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ public static function checkInArray($value, array $allowed)
throw new InvalidFormatException("Value '{$value}' not allowed: ".implode(', ', $allowed));
}
}
}

public static function checkAllowedValues(array $values, array $allowed)
{
$invalidFields = array_diff($values, $allowed);

if (count($invalidFields) > 0) {
throw new InvalidFormatException('These values are not allowed: '. implode(', ', $invalidFields));
}
}
}
72 changes: 72 additions & 0 deletions src/Request/Contactlists/Contactlist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types=1);

namespace SmartEmailing\v3\Request\Contactlists;

use SmartEmailing\v3\Api;
use SmartEmailing\v3\Exceptions\InvalidFormatException;
use SmartEmailing\v3\Request\AbstractRequest;

class Contactlist extends AbstractRequest implements \JsonSerializable
{

private const ALL_FIELDS = [
'id',
'name',
'category',
'publicname',
'sendername',
'senderemail',
'replyto',
'signature',
'segment_id',
];

/** @var string[] */
private $select = self::ALL_FIELDS;

/** @var int */
private $listId;

public function __construct(Api $api, int $listId)
{
parent::__construct($api);

$this->listId = $listId;
}

public function select(array $select): self
{
InvalidFormatException::checkAllowedValues($select, self::ALL_FIELDS);

$this->select = $select;
return $this;
}

protected function endpoint(): string
{
return 'contactlists/' . $this->listId;
}

protected function options(): array
{
return [
'json' => $this->jsonSerialize()
];
}

private function toArray(): array
{
return [
'select' => implode(',', $this->select),
];
}

/**
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}

}
28 changes: 28 additions & 0 deletions src/Request/Contactlists/ContactlistEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace SmartEmailing\v3\Request\Contactlists;

use SmartEmailing\v3\Api;

class ContactlistEndpoint
{

/** @var Api */
private $api;

public function __construct(Api $api)
{
$this->api = $api;
}

public function lists(): Contactlists
{
return new Contactlists($this->api);
}

public function get(int $listId): Contactlist
{
return new Contactlist($this->api, $listId);
}

}
61 changes: 61 additions & 0 deletions src/Request/Contactlists/Contactlists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types=1);

namespace SmartEmailing\v3\Request\Contactlists;

use SmartEmailing\v3\Exceptions\InvalidFormatException;
use SmartEmailing\v3\Request\AbstractRequest;

class Contactlists extends AbstractRequest implements \JsonSerializable
{

private const ALL_FIELDS = [
'id',
'name',
'category',
'publicname',
'sendername',
'senderemail',
'replyto',
'signature',
'segment_id',
];

/** @var string[] */
private $select = self::ALL_FIELDS;

public function select(array $select): self
{
InvalidFormatException::checkAllowedValues($select, self::ALL_FIELDS);

$this->select = $select;
return $this;
}

protected function endpoint(): string
{
return 'contactlists';
}

protected function options(): array
{
return [
'json' => $this->jsonSerialize()
];
}

private function toArray(): array
{
return [
'select' => implode(',', $this->select),
];
}

/**
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}

}
95 changes: 95 additions & 0 deletions src/Request/Email/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php declare(strict_types=1);

namespace SmartEmailing\v3\Request\Email;

use SmartEmailing\v3\Api;
use SmartEmailing\v3\Exceptions\PropertyRequiredException;
use SmartEmailing\v3\Request\AbstractRequest;

class Email extends AbstractRequest implements \JsonSerializable
{

/** @var string */
private $title;

/** @var string|null */
private $name;

/** @var string|null */
private $htmlBody;

/** @var string|null */
private $textBody;

public function __construct(Api $api, string $title)
{
parent::__construct($api);
$this->title = $title;
}

public function setName(string $name): self
{
$this->name = $name;
return $this;
}

public function setHtmlBody(string $htmlBody): self
{
$this->htmlBody = $htmlBody;
return $this;
}

public function setTextBody(string $textBody): self
{
$this->textBody = $textBody;
return $this;
}

protected function endpoint(): string
{
return 'emails';
}

protected function method()
{
return 'POST';
}

protected function options(): array
{
if ($this->htmlBody === null && $this->textBody === null) {
throw new PropertyRequiredException("At least one of the properties must be set: 'htmlBody', 'textBody'");
}

return [
'json' => $this->jsonSerialize()
];
}

private function toArray(): array
{
$data = [
'title' => $this->title,
'name' => $this->name ?: $this->title,
];

if ($this->htmlBody) {
$data['htmlbody'] = $this->htmlBody;
}

if ($this->textBody) {
$data['textbody'] = $this->textBody;
}

return $data;
}

/**
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}

}
23 changes: 23 additions & 0 deletions src/Request/Email/EmailsEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace SmartEmailing\v3\Request\Email;

use SmartEmailing\v3\Api;

class EmailsEndpoint
{

/** @var Api */
private $api;

public function __construct(Api $api)
{
$this->api = $api;
}

public function createNew(string $title): Email
{
return new Email($this->api, $title);
}

}
Loading

0 comments on commit ba2aa04

Please sign in to comment.