Send   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 7 1
A transform() 0 27 4
1
<?php
2
3
namespace ElasticEmail\Email;
4
5
use ElasticEmail\Client;
6
use ElasticEmail\Response;
7
8
/**
9
 * @see http://api.elasticemail.com/public/help#Email_Send
10
 */
11
class Send extends Response
12
{
13
    const URI = 'email/send';
14
15
    /** @var Client */
16
    private $client;
17
18
    public function __construct(Client $client)
19
    {
20
        $this->client = $client;
21
    }
22
23
    public function handle(array $params = [], $attachmentPaths = [])
24
    {
25
        $options = $this->transform($params, $attachmentPaths);
26
27
        $this->response = $this->client->request('POST', self::URI, $options);
28
29
        return $this;
30
    }
31
32
    protected function transform(array $params, array $attachmentPaths = [])
33
    {
34
        if (empty($attachmentPaths)) {
35
            return ['form_params' => $params];
36
        }
37
38
        $multipart = [];
39
40
        foreach ($params as $key => $value) {
41
            $multipart[] = [
42
                'name' => $key,
43
                'contents' => $value
44
            ];
45
        }
46
47
        foreach ($attachmentPaths as $path) {
48
            $pathParts = explode(DIRECTORY_SEPARATOR, $path);
49
            $filename = end($pathParts);
50
51
            $multipart[] = [
52
                'name' => $filename,
53
                'contents' => fopen($path, 'r'),
54
                'filename' => $filename
55
            ];
56
        }
57
58
        return ['multipart' => $multipart];
59
    }
60
}
61