1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\SqlDumper\Dumpers; |
4
|
|
|
|
5
|
|
|
use Cerbero\SqlDumper\Mail\SqlDumped; |
6
|
|
|
use Illuminate\Support\Facades\Config; |
7
|
|
|
use Illuminate\Support\Facades\Mail; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The email dumper. |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class EmailDumper implements DumperInterface |
15
|
|
|
{ |
16
|
|
|
use ConfigAware; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The html dumper. |
20
|
|
|
* |
21
|
|
|
* @var HtmlDumper |
22
|
|
|
*/ |
23
|
|
|
protected $html; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Instantiate the class. |
27
|
|
|
* |
28
|
|
|
* @param HtmlDumper $html |
29
|
|
|
*/ |
30
|
6 |
|
public function __construct(HtmlDumper $html) |
31
|
|
|
{ |
32
|
6 |
|
$this->html = $html; |
33
|
6 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Add the given query to dump |
37
|
|
|
* |
38
|
|
|
* @param string $query |
39
|
|
|
* @return DumperInterface |
40
|
|
|
*/ |
41
|
6 |
|
public function addQuery(string $query): DumperInterface |
42
|
|
|
{ |
43
|
6 |
|
$this->html->addQuery($query); |
44
|
|
|
|
45
|
6 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Add the query execution time to dump |
50
|
|
|
* |
51
|
|
|
* @param float $milliseconds |
52
|
|
|
* @return DumperInterface |
53
|
|
|
*/ |
54
|
6 |
|
public function addTime(float $milliseconds): DumperInterface |
55
|
|
|
{ |
56
|
6 |
|
$this->html->addTime($milliseconds); |
57
|
|
|
|
58
|
6 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Add the query caller to dump |
63
|
|
|
* |
64
|
|
|
* @param string $file |
65
|
|
|
* @param int $line |
66
|
|
|
* @return DumperInterface |
67
|
|
|
*/ |
68
|
6 |
|
public function addCaller(string $file, int $line): DumperInterface |
69
|
|
|
{ |
70
|
6 |
|
$this->html->addCaller($file, $line); |
71
|
|
|
|
72
|
6 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Add the given explanation rows to dump |
77
|
|
|
* |
78
|
|
|
* @param array $explanationRows |
79
|
|
|
* @return DumperInterface |
80
|
|
|
*/ |
81
|
6 |
|
public function addExplanations(array $explanationRows): DumperInterface |
82
|
|
|
{ |
83
|
6 |
|
$this->html->addExplanations($explanationRows); |
84
|
|
|
|
85
|
6 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Add a separator |
90
|
|
|
* |
91
|
|
|
* @return DumperInterface |
92
|
|
|
*/ |
93
|
6 |
|
public function addSeparator(): DumperInterface |
94
|
|
|
{ |
95
|
6 |
|
$this->html->addSeparator(); |
96
|
|
|
|
97
|
6 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Add the execution time of all queries to dump |
102
|
|
|
* |
103
|
|
|
* @param float $milliseconds |
104
|
|
|
* @return DumperInterface |
105
|
|
|
*/ |
106
|
6 |
|
public function addTotalTime(float $milliseconds): DumperInterface |
107
|
|
|
{ |
108
|
6 |
|
$this->html->addTotalTime($milliseconds); |
109
|
|
|
|
110
|
6 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Dump queries information |
115
|
|
|
* |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
6 |
|
public function dump() |
119
|
|
|
{ |
120
|
6 |
|
$recipient = $this->config('recipient', Config::get('mail.from.address')); |
121
|
|
|
|
122
|
6 |
|
if ($recipient === null) { |
123
|
3 |
|
throw new InvalidArgumentException('Missing recipient to send SQL queries dump to.'); |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
$method = $this->config('queue') ? 'queue' : 'send'; |
127
|
3 |
|
$template = $this->config('template', __DIR__ . '/../../stubs/template.html.stub'); |
128
|
3 |
|
$html = str_replace('{{content}}', $this->html->getContent(), file_get_contents($template)); |
129
|
3 |
|
$email = (new SqlDumped())->html($html); |
130
|
|
|
|
131
|
3 |
|
Mail::to($recipient)->$method($email); |
132
|
3 |
|
} |
133
|
|
|
} |
134
|
|
|
|