Test Setup Failed
Push — master ( bcd5a1...e5f8b7 )
by Daniel
02:52
created

Mail::getFrom()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Dacastro4\LaravelGmail\Services\Message;
4
5
use Carbon\Carbon;
6
use Dacastro4\LaravelGmail\GmailConnection;
7
use Dacastro4\LaravelGmail\Traits\HasDecodableBody;
8
use Dacastro4\LaravelGmail\Traits\HasHeaders;
9
use Dacastro4\LaravelGmail\Traits\Modifiable;
10
use Dacastro4\LaravelGmail\Traits\Replyable;
11
use Google_Service_Gmail;
12
use Illuminate\Support\Collection;
13
14
/**
15
 * Class SingleMessage
16
 * @package Dacastro4\LaravelGmail\services
17
 */
18
class Mail extends GmailConnection
19
{
20
	use HasDecodableBody,
21
		HasHeaders,
22
		Modifiable,
23
		Replyable {
24
		Replyable::__construct as private __rConstruct;
25
		Modifiable::__construct as private __mConstruct;
26
	}
27
28
	/**
29
	 * @var
30
	 */
31
	public $id;
32
33
	/**
34
	 * @var
35
	 */
36
	public $internalDate;
37
38
	/**
39
	 * @var
40
	 */
41
	public $labels;
42
43
	/**
44
	 * @var
45
	 */
46
	public $size;
47
48
	/**
49
	 * @var
50
	 */
51
	public $threatId;
52
53
	/**
54
	 * @var \Google_Service_Gmail_MessagePart
55
	 */
56
	public $payload;
57
58
	/**
59
	 * @var Google_Service_Gmail
60
	 */
61
	public $service;
62
63
	/**
64
	 * SingleMessage constructor.
65
	 *
66
	 * @param \Google_Service_Gmail_Message $message
67
	 * @param bool $preload
68
	 */
69
	public function __construct( \Google_Service_Gmail_Message $message = null, $preload = false )
70
	{
71
72
		$this->service = new Google_Service_Gmail( $this );
73
74
		$this->__rConstruct();
75
		$this->__mConstruct();
76
		parent::__construct();
77
78
		if(!is_null($message)) {
79
			if ( $preload ) {
80
				$message = $this->service->users_messages->get( 'me', $message->getId() );
81
			}
82
83
			$this->id = $message->getId();
84
			$this->internalDate = $message->getInternalDate();
85
			$this->labels = $message->getLabelIds();
86
			$this->size = $message->getSizeEstimate();
87
			$this->threatId = $message->getThreadId();
88
			$this->payload = $message->getPayload();
89
		}
90
	}
91
92
	/**
93
	 * Returns ID of the email
94
	 *
95
	 * @return string
96
	 */
97
	public function getId()
98
	{
99
		return $this->id;
100
	}
101
102
	/**
103
	 * Return a UNIX version of the date
104
	 *
105
	 * @return int UNIX date
106
	 */
107
	public function getInternalDate()
108
	{
109
		return $this->internalDate;
110
	}
111
112
	/**
113
	 * Returns the labels of the email
114
	 * Example: INBOX, STARRED, UNREAD
115
	 *
116
	 * @return mixed
117
	 */
118
	public function getLabels()
119
	{
120
		return $this->labels;
121
	}
122
123
	/**
124
	 * Returns approximate size of the email
125
	 *
126
	 * @return mixed
127
	 */
128
	public function getSize()
129
	{
130
		return $this->size;
131
	}
132
133
	/**
134
	 * Returns threat ID of the email
135
	 *
136
	 * @return string
137
	 */
138
	public function getThreatId()
139
	{
140
		return $this->threatId;
141
	}
142
143
	/**
144
	 * Returns all the headers of the email
145
	 *
146
	 * @return \Google_Service_Gmail_MessagePartHeader
147
	 */
148
	public function getHeaders()
149
	{
150
		return $this->payload->getHeaders();
151
	}
152
153
	/**
154
	 * Returns the subject of the email
155
	 *
156
	 * @return string
157
	 */
158
	public function getSubject()
159
	{
160
		return $this->getHeader( 'Subject' );
161
	}
162
163
	public function getFrom()
164
	{
165
166
		//TODO: Fix to work with multiple emails
167
		$email = $this->getHeader( 'From' );
168
169
		preg_match( '/<(.*)>/', $email, $matches );
170
171
		return isset( $matches[ 1 ] ) ? $matches[ 1 ] : null;
172
	}
173
174
	/**
175
	 * Returns the original date that the email was sent
176
	 *
177
	 * @return Carbon
178
	 */
179
	public function getDate()
180
	{
181
		return Carbon::parse( $this->getHeader( 'Date' ) );
0 ignored issues
show
Bug introduced by
It seems like $this->getHeader('Date') targeting Dacastro4\LaravelGmail\T...HasHeaders::getHeader() can also be of type array; however, Carbon\Carbon::parse() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
182
	}
183
184
	/**
185
	 * Returns email of the original recipient
186
	 *
187
	 * @return string
188
	 */
189
	public function getDeliveredTo()
190
	{
191
		return $this->getHeader( 'Delivered-To' );
192
	}
193
194
	/**
195
	 * @param bool $raw
196
	 *
197
	 * @return bool|string
198
	 */
199
	public function getPlainTextBody( $raw = false )
200
	{
201
		$content = $this->getBody();
202
203
		return $raw ? $content : $this->getDecodedBody( $content );
204
	}
205
206
	/**
207
	 * @return string base64 version of the body
208
	 */
209
	public function getRawPlainTextBody()
210
	{
211
		return $this->getPlainTextBody( true );
212
	}
213
214
	/**
215
	 * @param bool $raw
216
	 *
217
	 * @return string
218
	 */
219
	public function getHtmlBody( $raw = false )
220
	{
221
		$content = $this->getBody('text/html' );
222
223
		return $raw ? $content : $this->getDecodedBody( $content );
224
	}
225
226
	/**
227
	 * @return string base64 version of the body
228
	 */
229
	public function getRawHtmlBody()
230
	{
231
		return $this->getHtmlBody( true );
232
	}
233
234
	public function getAttachments()
235
	{
236
		$attachments = new Collection( [] );
237
		$parts = $this->payload->getParts();
238
239
		/** @var \Google_Service_Gmail_MessagePart $part */
240
		foreach ( $parts as $part ) {
241
242
			$body = $part->getBody();
243
244
			if ( $body->getAttachmentId() ) {
245
				$attachment = ( new Attachment( $this->getId(), $part ) );
246
				$attachments->push(
247
					$attachment
248
				);
249
			}
250
251
		}
252
253
		return $attachments;
254
255
	}
256
257
	/**
258
	 * @return boolean
259
	 */
260
	public function hasAttachments()
261
	{
262
		$attachments = 0;
263
		$parts = $this->payload->getParts();
264
265
		/**  @var \Google_Service_Gmail_MessagePart $part */
266
		foreach ( $parts as $part ) {
267
			$body = $part->getBody();
268
			if ( $body->getAttachmentId() ) {
269
				$attachments ++;
270
				break;
271
			}
272
		}
273
274
		return ! ! $attachments;
275
	}
276
277
	/**
278
	 * @param string $type
279
	 *
280
	 * @return \Google_Service_Gmail_MessagePart|null
281
	 */
282
	private function getBodyPart( $type = 'text/plain' )
283
	{
284
		$body = $this->payload->getParts();
285
286
		if ( isset( $body[ 0 ] ) ) {
287
288
			$parts = $body[ 0 ]->getParts();
289
290
			/** @var \Google_Service_Gmail_MessagePart $part */
291
			foreach ( $parts as $part ) {
292
				if ( $part->getMimeType() === $type ) {
293
					break;
294
				}
295
			}
296
297
			return $part ?: null;
0 ignored issues
show
Bug introduced by
The variable $part seems to be defined by a foreach iteration on line 291. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
298
299
		}
300
301
		return null;
302
	}
303
304
	public function getBody( $type = 'text/plain' )
305
	{
306
		$part = $this->getBodyPart($type);
307
		$body = $part->getBody();
308
309
		return $body->getData();
310
	}
311
312
	/**
313
	 * Get's the gmail information from the Mail
314
	 *
315
	 * @return Mail
316
	 */
317
	public function load()
318
	{
319
		$message = $this->service->users_messages->get( 'me', $this->getId() );
320
321
		return new self( $message );
322
	}
323
}