|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Qodeboy\Mailman\Logger; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
6
|
|
|
use Qodeboy\Mailman\Contracts\MailmanSwiftMessageAdapter; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class MailmanFilesystemLogger. |
|
10
|
|
|
*/ |
|
11
|
|
|
class MailmanFilesystemLogger extends AbstractMailmanLogger |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Base path where email logs are stored. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $storagePath; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* FileSystem adapter. |
|
22
|
|
|
* |
|
23
|
|
|
* @var Filesystem |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $fileSystem; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* MailmanFilesystemLogger constructor. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->storagePath = config('mailman.storage.filesystem.path'); |
|
33
|
|
|
$this->fileSystem = app(Filesystem::class); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Log given Swift_Mime_Message email message. |
|
38
|
|
|
* |
|
39
|
|
|
* @param MailmanSwiftMessageAdapter $message |
|
40
|
|
|
* |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
*/ |
|
43
|
|
|
public function log(MailmanSwiftMessageAdapter $message) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->prepareStorage($message); |
|
46
|
|
|
|
|
47
|
|
|
$this->fileSystem->put( |
|
48
|
|
|
$this->getMessageLogFilePath($message).'.html', |
|
49
|
|
|
$this->getMessageHTMLContent($message) |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
$this->fileSystem->put( |
|
53
|
|
|
$this->getMessageLogFilePath($message).'.eml', |
|
54
|
|
|
$this->getMessageEMLContent($message) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Generate a human readable HTML comment with message info. |
|
60
|
|
|
* |
|
61
|
|
|
* @param MailmanSwiftMessageAdapter $message |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function getMessageInfo(MailmanSwiftMessageAdapter $message) |
|
66
|
|
|
{ |
|
67
|
|
|
return sprintf( |
|
|
|
|
|
|
68
|
|
|
"<!--\nStatus: %s, \nMessageID: %s, \nContent-Type: %s, \nFrom:%s, \nTo:%s, \nReply-To:%s, \nCC:%s, \nBCC:%s, \nSubject:%s\n-->\n", |
|
69
|
|
|
$message->getStatus(), |
|
70
|
|
|
$message->getId(), |
|
71
|
|
|
$message->getContentType(), |
|
72
|
|
|
json_encode($message->getFrom()), |
|
73
|
|
|
json_encode($message->getTo()), |
|
74
|
|
|
json_encode($message->getReplyTo()), |
|
75
|
|
|
json_encode($message->getCc()), |
|
76
|
|
|
json_encode($message->getBcc()), |
|
77
|
|
|
$message->getSubject() |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the HTML content for the log file. |
|
83
|
|
|
* |
|
84
|
|
|
* @param MailmanSwiftMessageAdapter $message |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getMessageHTMLContent(MailmanSwiftMessageAdapter $message) |
|
89
|
|
|
{ |
|
90
|
|
|
$messageInfo = $this->getMessageInfo($message); |
|
91
|
|
|
|
|
92
|
|
|
return $messageInfo.$message->getBody(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the EML content for the log file. |
|
97
|
|
|
* |
|
98
|
|
|
* @param MailmanSwiftMessageAdapter $message |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function getMessageEMLContent(MailmanSwiftMessageAdapter $message) |
|
103
|
|
|
{ |
|
104
|
|
|
return $message->toString(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get the path to the email log file. |
|
109
|
|
|
* |
|
110
|
|
|
* @param MailmanSwiftMessageAdapter $message |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function getMessageLogFilePath(MailmanSwiftMessageAdapter $message) |
|
115
|
|
|
{ |
|
116
|
|
|
$messageLogDirectory = $this->getMessageLogDirectoryPath($message); |
|
117
|
|
|
$messageLogFileName = str_replace(['@', '.'], ['_at_', '_'], $message->getId()); |
|
118
|
|
|
|
|
119
|
|
|
return $messageLogDirectory.'/'.str_slug($messageLogFileName); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get the path to the email log directory. |
|
124
|
|
|
* |
|
125
|
|
|
* @param MailmanSwiftMessageAdapter $message |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function getMessageLogDirectoryPath(MailmanSwiftMessageAdapter $message) |
|
130
|
|
|
{ |
|
131
|
|
|
list($messageId) = explode('@', $message->getId()); |
|
132
|
|
|
|
|
133
|
|
|
return storage_path($this->storagePath.'/'.$messageId); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Create required directories for logging. |
|
138
|
|
|
* |
|
139
|
|
|
* @param MailmanSwiftMessageAdapter $message |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function prepareStorage(MailmanSwiftMessageAdapter $message) |
|
142
|
|
|
{ |
|
143
|
|
|
$messageLogStorageDirectory = storage_path($this->storagePath); |
|
144
|
|
|
if (! $this->fileSystem->exists($messageLogStorageDirectory)) { |
|
145
|
|
|
$this->fileSystem->makeDirectory($messageLogStorageDirectory); |
|
146
|
|
|
$this->fileSystem->put($messageLogStorageDirectory.'/.gitignore', "*\n!.gitignore"); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$messageLogDirectory = $this->getMessageLogDirectoryPath($message); |
|
150
|
|
|
if (! $this->fileSystem->exists($messageLogDirectory)) { |
|
151
|
|
|
$this->fileSystem->makeDirectory($messageLogDirectory); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.