Skip to content

Commit 83b94c6

Browse files
committed
code refactor
1 parent 6e9022b commit 83b94c6

18 files changed

+628
-314
lines changed

src/Config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Config
99
*
1010
* @return int
1111
*/
12-
public static function maxRetryCount()
12+
public static function maxAttemptCount()
1313
{
1414
return max(config('laravel-database-emails.retry.attempts', 1), 3);
1515
}
@@ -59,4 +59,4 @@ public static function cronjobEmailLimit()
5959
{
6060
return config('laravel-database-emails.limit', 20);
6161
}
62-
}
62+
}

src/CreateEmailTableCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class CreateEmailTableCommand extends Command
4040
*
4141
* @param \Illuminate\Filesystem\Filesystem $files
4242
* @param \Illuminate\Support\Composer $composer
43-
* @return void
4443
*/
4544
public function __construct(Filesystem $files, Composer $composer)
4645
{

src/Decorators/EmailDecorator.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/Decorators/EncryptEmail.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/Decorators/PrepareEmail.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/Email.php

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,39 @@
22

33
namespace Buildcode\LaravelDatabaseEmails;
44

5-
use Illuminate\Contracts\Encryption\DecryptException;
65
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Mail\Message;
77
use Illuminate\Support\Facades\Event;
88
use Illuminate\Support\Facades\Mail;
99
use Carbon\Carbon;
1010
use Exception;
1111

12+
/**
13+
* @property $id
14+
* @property $label
15+
* @property $recipient
16+
* @property $cc
17+
* @property $bcc
18+
* @property $subject
19+
* @property $view
20+
* @property $variables
21+
* @property $body
22+
* @property $attempts
23+
* @property $sending
24+
* @property $failed
25+
* @property $error
26+
* @property $encrypted
27+
* @property $scheduled_at
28+
* @property $sent_at
29+
* @property $delivered_at
30+
*/
1231
class Email extends Model
1332
{
33+
/**
34+
* Make sure all encrypted attributes are decrypted.
35+
*/
36+
use HasEncryptedAttributes;
37+
1438
/**
1539
* The table in which the e-mails are stored.
1640
*
@@ -62,7 +86,7 @@ public function getLabel()
6286
*/
6387
public function getRecipient()
6488
{
65-
return $this->getEmailProperty('recipient');
89+
return $this->recipient;
6690
}
6791

6892
/**
@@ -72,12 +96,6 @@ public function getRecipient()
7296
*/
7397
public function getCc()
7498
{
75-
if ($this->exists) {
76-
$cc = $this->getEmailProperty('cc');
77-
78-
return json_decode($cc, 1);
79-
}
80-
8199
return $this->cc;
82100
}
83101

@@ -88,12 +106,6 @@ public function getCc()
88106
*/
89107
public function getBcc()
90108
{
91-
if ($this->exists) {
92-
$bcc = $this->getEmailProperty('bcc');
93-
94-
return json_decode($bcc, 1);
95-
}
96-
97109
return $this->bcc;
98110
}
99111

@@ -104,7 +116,7 @@ public function getBcc()
104116
*/
105117
public function getSubject()
106118
{
107-
return $this->getEmailProperty('subject');
119+
return $this->subject;
108120
}
109121

110122
/**
@@ -125,16 +137,6 @@ public function getView()
125137
*/
126138
public function getVariables()
127139
{
128-
if ($this->exists) {
129-
$var = $this->getEmailProperty('variables');
130-
131-
return json_decode($var, 1);
132-
}
133-
134-
if (is_string($this->variables)) {
135-
return json_decode($this->variables, 1);
136-
}
137-
138140
return $this->variables;
139141
}
140142

@@ -145,7 +147,7 @@ public function getVariables()
145147
*/
146148
public function getBody()
147149
{
148-
return $this->getEmailProperty('body');
150+
return $this->body;
149151
}
150152

151153
/**
@@ -251,7 +253,7 @@ public function isScheduled()
251253
*/
252254
public function isEncrypted()
253255
{
254-
return !!$this->encrypted;
256+
return !!$this->getOriginal('encrypted');
255257
}
256258

257259
/**
@@ -274,25 +276,6 @@ public function hasFailed()
274276
return $this->failed == 1;
275277
}
276278

277-
/**
278-
* Get a decrypted property.
279-
*
280-
* @param string $property
281-
* @return mixed
282-
*/
283-
private function getEmailProperty($property)
284-
{
285-
if ($this->exists && $this->isEncrypted()) {
286-
try {
287-
return decrypt($this->{$property});
288-
} catch (DecryptException $e) {
289-
return '';
290-
}
291-
}
292-
293-
return $this->{$property};
294-
}
295-
296279
/**
297280
* Mark the e-mail as sending.
298281
*
@@ -318,6 +301,8 @@ public function markAsSent()
318301
$this->update([
319302
'sending' => 0,
320303
'sent_at' => $now,
304+
'failed' => 0,
305+
'error' => '',
321306
]);
322307
}
323308

@@ -352,8 +337,8 @@ public function send()
352337
if (app()->runningUnitTests()) {
353338
Event::dispatch('before.send');
354339
}
355-
356-
Mail::send([], [], function ($message) {
340+
341+
Mail::send([], [], function (Message $message) {
357342
$message->to($this->getRecipient())
358343
->cc($this->hasCc() ? $this->getCc() : [])
359344
->bcc($this->hasBcc() ? $this->getBcc() : [])

0 commit comments

Comments
 (0)