領収証発行サービス
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

201 line
5.0KB

  1. <?php
  2. namespace App\Email;
  3. use App\Exceptions\AppCommonException;
  4. use App\Models\Email;
  5. use App\Models\EmailAttachment;
  6. use App\Models\User;
  7. use App\Util\DateUtil;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Database\Eloquent\Collection;
  10. use Illuminate\Mail\Mailable;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Support\Facades\Mail;
  13. use Illuminate\Support\Str;
  14. abstract class BaseEmailer extends Mailable
  15. {
  16. use Queueable, SerializesModels;
  17. protected $configRoot = 'mail.mailers.smtp_ht.';
  18. protected $casts = [];
  19. protected User|null $__user = null;
  20. protected string $__email = "";
  21. protected string|null $__userId = null;
  22. protected string|null $__contractId = null;
  23. protected string|null $__receiptIssuingOrderId = null;
  24. /**
  25. * 添付ファイル
  26. * @var Collection<int, EmailAttachment>|null
  27. */
  28. protected ?Collection $__attachments = null;
  29. public function sendEmail(string $email)
  30. {
  31. $this->__email = $email;
  32. Mail::to($email)
  33. ->send($this);
  34. }
  35. public function setUser(User $user)
  36. {
  37. $this->__userId = $user->id;
  38. $this->__email = $user->email;
  39. return $this;
  40. }
  41. public function setEmail(string $target)
  42. {
  43. $this->__email = $target;
  44. return $this;
  45. }
  46. public function setUserId(string $userId)
  47. {
  48. $this->__userId = $userId;
  49. return $this;
  50. }
  51. public function setContractId(string $contractId)
  52. {
  53. $this->__contractId = $contractId;
  54. return $this;
  55. }
  56. public function setReceiptIssuingOrderId(string $receiptIssuingOrderId)
  57. {
  58. $this->__receiptIssuingOrderId = $receiptIssuingOrderId;
  59. return $this;
  60. }
  61. public function build()
  62. {
  63. $this->text($this->getTemplateName())
  64. ->subject($this->getSubject())
  65. ->with($this->getParams());
  66. // 添付ファイル処理
  67. if ($this->__attachments !== null) {
  68. $count = $this->__attachments->count();
  69. foreach ($this->__attachments as $attachment) {
  70. $filepath = $attachment->filepath;
  71. $as = $attachment->send_filename;
  72. $mime = $attachment->mime;
  73. $this->attach($filepath, [
  74. 'as' => $as,
  75. 'mime' => $mime,
  76. ]);
  77. }
  78. }
  79. return $this;
  80. }
  81. public function setConfigDefault()
  82. {
  83. $root = $this->configRoot;
  84. config([
  85. $root . "transport" => "smtp",
  86. $root . "host" => env('MAIL_HOST', 'smtp.mailgun.org'),
  87. $root . 'port' => env('MAIL_PORT', 587),
  88. $root . 'encryption' => env('MAIL_ENCRYPTION', 'tls'),
  89. $root . 'username' => env('MAIL_USERNAME'),
  90. $root . 'password' => env('MAIL_PASSWORD'),
  91. $root . 'timeout' => null,
  92. $root . 'auth_mode' => null,
  93. $root . 'verify_peer' => false,
  94. ]);
  95. }
  96. public function setConfig(array $config)
  97. {
  98. $root = $this->configRoot;
  99. $ret = [];
  100. foreach ($config as $key => $val) {
  101. $ret[$root . $key] = $val;
  102. }
  103. config($ret);
  104. }
  105. protected function setValues(\stdClass|array $data)
  106. {
  107. foreach ($data as $key => $val) {
  108. $this->setValue($key, $val);
  109. }
  110. }
  111. protected function setValue(string $key, $value)
  112. {
  113. $val = $value;
  114. $camel = Str::camel($key);
  115. $snake = Str::snake($key);
  116. $property = "";
  117. if (property_exists($this, $camel)) {
  118. $property = $camel;
  119. } else if (property_exists($this, $snake)) {
  120. $property = $snake;
  121. } else {
  122. return;
  123. }
  124. if (data_get($this->casts, $property) === 'datetime') {
  125. $this->$property = DateUtil::parse($val);
  126. } else {
  127. $this->$property = $val;
  128. }
  129. }
  130. public function makeModel(): Email
  131. {
  132. if ($this->__email === "") {
  133. throw new AppCommonException("Email宛先不明");
  134. }
  135. $model = new Email();
  136. $model->subject = $this->getSubject();
  137. $model->content = $this->render();
  138. $model->type = get_class($this);
  139. $model->email = $this->__email;
  140. $model->contract_id = $this->__contractId;
  141. $model->user_id = $this->__userId;
  142. $model->receipt_issuing_order_id = $this->__receiptIssuingOrderId;
  143. return $model;
  144. }
  145. abstract public function getTemplateName(): string;
  146. abstract public function getSubject(): string;
  147. abstract public function getParams(): array;
  148. /**
  149. * 画面のURLを生成する
  150. *
  151. * @param array|string $path
  152. * @return string
  153. */
  154. protected function getAppUrl(array|string $path): string
  155. {
  156. $elements = [config("app.url")];
  157. if (is_array($path)) {
  158. $elements = array_merge($elements, $path);
  159. } else {
  160. $elements[] = $path;
  161. }
  162. return implode(
  163. "/",
  164. $elements,
  165. );
  166. }
  167. }