Test Setup Failed
Push — master ( 4eae4b...83f7a2 )
by Daniel
01:54
created

Replyable   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 332
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 4
dl 0
loc 332
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A to() 0 7 1
A from() 0 7 1
A cc() 0 7 1
A bcc() 0 7 1
A subject() 0 6 1
A message() 0 6 1
A attach() 0 18 2
A priority() 0 6 1
A optionalParameters() 0 6 1
A reply() 0 8 1
A send() 0 6 1
A setHeader() 0 7 1
A getMessageBody() 0 22 2
A base64_encode() 0 4 1
A emailList() 0 8 2
A setReplySubject() 0 6 2
A setReplyThreat() 0 9 2
getThreatId() 0 1 ?
1
<?php
2
3
namespace Dacastro4\LaravelGmail\Traits;
4
5
use Dacastro4\LaravelGmail\Services\Message\Mail;
6
use Google_Service_Gmail;
7
use Google_Service_Gmail_Message;
8
use Illuminate\Support\Facades\Storage;
9
use Swift_Attachment;
10
use Swift_Message;
11
12
/**
13
 * @property Google_Service_Gmail $service
14
 */
15
trait Replyable
16
{
17
18
	private $swiftMessage;
19
20
	/**
21
	 * Gmail optional parameters
22
	 *
23
	 * @var array
24
	 */
25
	private $parameters = [];
26
27
	/**
28
	 * Text or html message to send
29
	 *
30
	 * @var string
31
	 */
32
	private $message;
33
34
	/**
35
	 * Subject of the email
36
	 *
37
	 * @var string
38
	 */
39
	private $subject;
40
41
	/**
42
	 * Sender's email
43
	 *
44
	 * @var string
45
	 */
46
	private $from;
47
48
	/**
49
	 * Sender's name
50
	 *
51
	 * @var  string
52
	 */
53
	private $nameFrom;
54
55
	/**
56
	 * Email of the recipient
57
	 *
58
	 * @var string|array
59
	 */
60
	private $to;
61
62
	/**
63
	 * Name of the recipient
64
	 *
65
	 * @var string
66
	 */
67
	private $nameTo;
68
69
	/**
70
	 * Single email or array of email for a carbon copy
71
	 *
72
	 * @var array|string
73
	 */
74
	private $cc;
75
76
	/**
77
	 * Name of the recipient
78
	 *
79
	 * @var string
80
	 */
81
	private $nameCc;
82
83
	/**
84
	 * Single email or array of email for a blind carbon copy
85
	 *
86
	 * @var array|string
87
	 */
88
	private $bcc;
89
90
	/**
91
	 * Name of the recipient
92
	 *
93
	 * @var string
94
	 */
95
	private $nameBcc;
96
97
	/**
98
	 * List of attachments
99
	 *
100
	 * @var array
101
	 */
102
	private $attachments = [];
103
104
	private $priority = 2;
105
106
	public function __construct()
107
	{
108
		$this->swiftMessage = new Swift_Message();
109
	}
110
111
	/**
112
	 * Receives the recipient's
113
	 * If multiple recipients will receive the message an array should be used.
114
	 * Example: array('[email protected]', '[email protected]' => 'A name')
115
	 *
116
	 * If $name is passed and the first parameter is a string, this name will be
117
	 * associated with the address.
118
	 *
119
	 * @param string|array $to
120
	 *
121
	 * @param string|null $name
122
	 *
123
	 * @return $this
124
	 */
125
	public function to( $to, $name = null )
126
	{
127
		$this->to = $to;
128
		$this->nameTo = $name;
129
130
		return $this;
131
	}
132
133
	public function from( $from, $name = null )
134
	{
135
		$this->from = $from;
136
		$this->nameTo = $name;
137
138
		return $this;
139
	}
140
141
	/**
142
	 * @param array|string $cc
143
	 *
144
	 * @param string|null $name
145
	 *
146
	 * @return $this
147
	 */
148
	public function cc( $cc, $name = null )
149
	{
150
		$this->cc = $this->emailList( $cc );
151
		$this->nameCc = $name;
152
153
		return $this;
154
	}
155
156
	/**
157
	 * @param array|string $bcc
158
	 *
159
	 * @param string|null $name
160
	 *
161
	 * @return $this
162
	 */
163
	public function bcc( $bcc, $name = null )
164
	{
165
		$this->bcc = $this->emailList( $bcc );
166
		$this->nameBcc = $name;
167
168
		return $this;
169
	}
170
171
	/**
172
	 * @param string $subject
173
	 *
174
	 * @return $this
175
	 */
176
	public function subject( $subject )
177
	{
178
		$this->subject = $subject;
179
180
		return $this;
181
	}
182
183
	/**
184
	 * @param string $message
185
	 *
186
	 * @return $this
187
	 */
188
	public function message( $message )
189
	{
190
		$this->message = $message;
191
192
		return $this;
193
	}
194
195
	/**
196
	 * Attaches new file to the email from the Storage folder
197
	 *
198
	 * @param $path
199
	 *
200
	 * @return $this
201
	 * @throws \Exception
202
	 */
203
	public function attach( $path )
204
	{
205
206
		$file = [];
207
208
		//TODO: Solve the attach issue. Which files should I attach? UploadFile it's an option.
209
		if ( Storage::has( $path ) ) {
210
			$content = Storage::get( $path );
211
			$name = Storage::name( $path );
212
			$file[ $name ] = $content;
213
		} else {
214
			throw new \Exception( 'File does not exists.' );
215
		}
216
217
		array_push( $this->attachments, $file );
218
219
		return $this;
220
	}
221
222
	/**
223
	 * The value is an integer where 1 is the highest priority and 5 is the lowest.
224
	 *
225
	 * @param int $priority
226
	 *
227
	 * @return $this
228
	 */
229
	public function priority( $priority )
230
	{
231
		$this->priority = $priority;
232
233
		return $this;
234
	}
235
236
	/**
237
	 * @param array $parameters
238
	 *
239
	 * @return $this
240
	 */
241
	public function optionalParameters( array $parameters )
242
	{
243
		$this->parameters = $parameters;
244
245
		return $this;
246
	}
247
248
	/**
249
	 * Reply to a specific email
250
	 *
251
	 * @return Mail
252
	 */
253
	public function reply()
254
	{
255
		$this->setReplyThreat();
256
		$this->setReplySubject();
257
		$body = $this->getMessageBody();
258
259
		return new Mail( $this->service->users_messages->send( 'me', $body, $this->parameters ) );
260
	}
261
262
	/**
263
	 * Sends a new email
264
	 *
265
	 * @return Mail
266
	 */
267
	public function send()
268
	{
269
		$body = $this->getMessageBody();
270
271
		return new Mail( $this->service->users_messages->send( 'me', $body, $this->parameters ) );
272
	}
273
274
	/**
275
	 * Add a header to the email
276
	 *
277
	 * @param string $header
278
	 * @param string $value
279
	 */
280
	public function setHeader( $header, $value )
281
	{
282
		$headers = $this->swiftMessage->getHeaders();
283
284
		$headers->addTextHeader( $header, $value );
285
286
	}
287
288
	/**
289
	 * @return Google_Service_Gmail_Message
290
	 */
291
	private function getMessageBody()
292
	{
293
		$body = new Google_Service_Gmail_Message();
294
295
		$this->swiftMessage
296
			->setSubject( $this->subject )
297
			->setFrom( $this->from, $this->nameFrom )
298
			->setTo( $this->to, $this->nameTo )
299
			->setCc( $this->cc, $this->nameCc )
300
			->setBcc( $this->bcc, $this->nameBcc )
301
			->setBody( $this->message, 'text/html' )
302
			->setPriority( $this->priority );
303
304
		foreach ( $this->attachments as $file ) {
305
			$this->swiftMessage
306
				->attach( Swift_Attachment::fromPath( $file->path )->setFilename( $file->name ) );
307
		}
308
309
		$body->setRaw( $this->base64_encode( $this->swiftMessage->toString() ) );
310
311
		return $body;
312
	}
313
314
	private function base64_encode( $data )
315
	{
316
		return rtrim( strtr( base64_encode( $data ), '+/', '-_' ), '=' );
317
	}
318
319
	private function emailList( $list )
320
	{
321
		if ( is_array( $list ) ) {
322
			return implode( ', ', $list );
323
		} else {
324
			return $list;
325
		}
326
	}
327
328
	private function setReplySubject()
329
	{
330
		if ( ! $this->subject ) {
331
			$this->subject = $this->getSubject();
0 ignored issues
show
Bug introduced by
The method getSubject() does not exist on Dacastro4\LaravelGmail\Traits\Replyable. Did you maybe mean subject()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
332
		}
333
	}
334
335
	private function setReplyThreat()
336
	{
337
		$threatId = $this->getThreatId();
338
		if ( $threatId ) {
339
			$this->setHeader( 'In-Reply-To', $this->getHeader( 'In-Reply-To' ) );
0 ignored issues
show
Bug introduced by
It seems like getHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
340
			$this->setHeader( 'References', $this->getHeader( 'References' ) );
0 ignored issues
show
Bug introduced by
It seems like getHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
341
			$this->setHeader( 'Message-ID', $this->getHeader( 'Message-ID' ) );
0 ignored issues
show
Bug introduced by
It seems like getHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
342
		}
343
	}
344
345
	public abstract function getThreatId();
346
}