領収証発行サービス
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

212 rindas
5.3KB

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