領収証発行サービス
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

178 lines
4.3KB

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