1
|
|
|
<?php namespace Camelcased\Postmark\Inbound; |
2
|
|
|
|
3
|
|
|
use Camelcased\Postmark\Inbound\Attachment as Attachment; |
4
|
|
|
|
5
|
|
|
class Email { |
6
|
|
|
/** |
7
|
|
|
* @var array |
8
|
|
|
*/ |
9
|
|
|
protected $data; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $attachments = []; |
15
|
|
|
|
16
|
|
|
public function __construct(array $data) |
17
|
|
|
{ |
18
|
|
|
$this->data = $data; |
19
|
|
|
|
20
|
|
|
// Does the email contain attachments? |
21
|
|
|
if ($this->hasAttachments()) |
22
|
|
|
{ |
23
|
|
|
// Then setup the array with Attachment objects |
24
|
|
|
$this->setAttachments(); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Maps undefined functions to the correct field in the email |
30
|
|
|
* |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function __call($name, $arguments) |
34
|
|
|
{ |
35
|
|
|
return $this->data[$name]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Check for if the email has any attachments |
40
|
|
|
* |
41
|
|
|
* @return boolean |
42
|
|
|
*/ |
43
|
|
|
public function hasAttachments() |
44
|
|
|
{ |
45
|
|
|
if (array_key_exists('Attachments', $this->data)) { |
46
|
|
|
return count($this->data["Attachments"]) > 0 ? true : false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Is the body in the email plain text? |
54
|
|
|
* |
55
|
|
|
* @return boolean |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function bodyIsText() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
if ($this->data['body'] == strip_tags($this->data['body'])) |
60
|
|
|
{ |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Is the body in the email HTML? |
69
|
|
|
* |
70
|
|
|
* @return boolean |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function bodyIsHtml() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
if ($this->data['body'] != strip_tags($this->data['body'])) |
75
|
|
|
{ |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Return the array of Attachment objects |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function attachments() |
88
|
|
|
{ |
89
|
|
|
return $this->attachments; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Loops through the attachments and creates an Attachment object for each attachemnt then pushes each object into an array. |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
private function setAttachments() |
98
|
|
|
{ |
99
|
|
|
foreach ($this->data['Attachments'] as $attachment) |
100
|
|
|
{ |
101
|
|
|
array_push($this->attachments, new Attachment($attachment)); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.