Skip to content

Commit ac33784

Browse files
committed
added batch action
1 parent 751fbe7 commit ac33784

File tree

4 files changed

+228
-1
lines changed

4 files changed

+228
-1
lines changed

src/Api/Batch.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace MailerLiteApi\Api;
4+
5+
use MailerLiteApi\Common\ApiAbstract;
6+
use MailerLiteApi\Common\BatchRequest;
7+
use MailerLiteApi\Exceptions\MailerLiteSdkException;
8+
9+
/**
10+
* Class Batch
11+
*
12+
* @package MailerLiteApi\Api
13+
*/
14+
class Batch extends ApiAbstract
15+
{
16+
17+
protected $endpoint = 'batch';
18+
19+
/**
20+
* @param BatchRequest[] $requests
21+
*
22+
* @return mixed
23+
*/
24+
public function send(array $requests)
25+
{
26+
if ( ! count($requests)) {
27+
throw new MailerLiteSdkException("Provide at least one request");
28+
}
29+
30+
foreach ($requests as &$request) {
31+
if ( ! is_object($request) || get_class($request) != BatchRequest::class) {
32+
throw new MailerLiteSdkException("All requests must be of type ".BatchRequest::class);
33+
}
34+
35+
$request = $request->toArray();
36+
}
37+
38+
$response = $this->restClient->post($this->endpoint, [
39+
'requests' => $requests,
40+
]);
41+
42+
return $response['body'];
43+
}
44+
45+
}

src/Common/BatchRequest.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace MailerLiteApi\Common;
4+
5+
/**
6+
* Class BatchRequest
7+
*
8+
* @package MailerLiteApi\Common
9+
*/
10+
class BatchRequest
11+
{
12+
13+
const METHOD_GET = 'GET';
14+
15+
const METHOD_POST = 'POST';
16+
17+
const METHOD_PUT = 'PUT';
18+
19+
const METHOD_DELETE = 'DELETE';
20+
21+
/** @var string $method */
22+
protected $method = self::METHOD_GET;
23+
24+
/** @var string $path */
25+
protected $path = '';
26+
27+
/** @var null|array $body */
28+
protected $body = null;
29+
30+
/**
31+
* @return string
32+
*/
33+
public function getMethod(): string
34+
{
35+
return $this->method;
36+
}
37+
38+
/**
39+
* @param string $method
40+
*
41+
* @return BatchRequest
42+
*/
43+
public function setMethod(string $method): BatchRequest
44+
{
45+
$this->method = $method;
46+
47+
return $this;
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
public function getPath(): string
54+
{
55+
return $this->path;
56+
}
57+
58+
/**
59+
* @param string $path
60+
*
61+
* @return BatchRequest
62+
*/
63+
public function setPath(string $path): BatchRequest
64+
{
65+
$this->path = $path;
66+
67+
return $this;
68+
}
69+
70+
/**
71+
* @return null
72+
*/
73+
public function getBody()
74+
{
75+
return $this->body;
76+
}
77+
78+
/**
79+
* @param null|array $body
80+
*
81+
* @return BatchRequest
82+
*/
83+
public function setBody($body)
84+
{
85+
$this->body = $body;
86+
87+
return $this;
88+
}
89+
90+
/**
91+
* @return array
92+
*/
93+
public function toArray(): array
94+
{
95+
$array = [
96+
'method' => $this->getMethod(),
97+
'path' => $this->getPath(),
98+
];
99+
100+
if ($body = $this->getBody()) {
101+
$array['body'] = $body;
102+
}
103+
104+
return $array;
105+
}
106+
107+
}

src/MailerLite.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ public function segments()
102102
return new \MailerLiteApi\Api\Segments($this->restClient);
103103
}
104104

105+
/**
106+
* @return \MailerLiteApi\Api\Batch
107+
*/
108+
public function batch()
109+
{
110+
return new \MailerLiteApi\Api\Batch($this->restClient);
111+
}
112+
105113
/**
106114
* @param string $version
107115
* @return string
@@ -111,4 +119,4 @@ public function getBaseUrl($version = ApiConstants::VERSION)
111119
return ApiConstants::BASE_URL . $version . '/';
112120
}
113121

114-
}
122+
}

tests/BatchTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace MailerLiteApi\Tests;
4+
5+
use MailerLiteApi\Common\BatchRequest;
6+
use MailerLiteApi\Exceptions\MailerLiteSdkException;
7+
use MailerLiteApi\MailerLite;
8+
9+
/**
10+
* Class BatchTest
11+
*
12+
* @package MailerLiteApi\Tests
13+
*/
14+
class BatchTest extends MlTestCase
15+
{
16+
17+
/** @var \MailerLiteApi\Api\Batch */
18+
protected $api;
19+
20+
protected function setUp(): void
21+
{
22+
$this->api = (new MailerLite(API_KEY))->batch();
23+
}
24+
25+
/** @test * */
26+
public function at_least_one_request_is_provided_exception()
27+
{
28+
$this->expectException(MailerLiteSdkException::class);
29+
$this->api->send([]);
30+
}
31+
32+
/** @test * */
33+
public function provided_requests_must_be_batch_request_class()
34+
{
35+
$this->expectException(MailerLiteSdkException::class);
36+
$this->api->send([
37+
new \stdClass(),
38+
]);
39+
}
40+
41+
/** @test * */
42+
public function send_batch()
43+
{
44+
$requests = [];
45+
$batchOne = (new BatchRequest())
46+
->setMethod(BatchRequest::METHOD_POST)
47+
->setPath('/api/v2/subscribers')
48+
->setBody([
49+
'email' => '[email protected]',
50+
]);
51+
52+
$batchTwo = (new BatchRequest())
53+
->setMethod(BatchRequest::METHOD_POST)
54+
->setPath('/api/v2/subscribers')
55+
->setBody([
56+
'email' => '[email protected]',
57+
]);
58+
59+
$requests[] = $batchOne;
60+
$requests[] = $batchTwo;
61+
62+
$result = $this->api->send($requests);
63+
64+
$this->assertTrue($result[0]->body->email == $batchOne->getBody()['email']);
65+
$this->assertTrue($result[1]->body->email == $batchTwo->getBody()['email']);
66+
}
67+
}

0 commit comments

Comments
 (0)