Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

138 lines
3.4KB

  1. <?php
  2. namespace App\Logic;
  3. use App\Codes\EnvironmentName;
  4. use App\Events\Email\ConfirmEvent;
  5. use App\Exceptions\AppCommonException;
  6. use App\Email\BaseEmailer;
  7. use App\Models\Email;
  8. use App\Models\EmailAttachment;
  9. use Exception;
  10. use Illuminate\Database\Eloquent\Collection;
  11. use Illuminate\Support\Carbon;
  12. use Illuminate\Support\Str;
  13. use Validator;
  14. class EmailManager
  15. {
  16. private Email $model;
  17. private bool $canSend = false;
  18. /**
  19. * @var Collection<int, EmailAttachment>|null
  20. */
  21. private ?Collection $attachments = null;
  22. public function __construct(int|BaseEmailer $param)
  23. {
  24. if (is_numeric($param)) {
  25. $this->model = Email::lockForUpdate()->findOrfail($param);
  26. $this->canSend = $this->model->send_datetime === null;
  27. if (!$this->checkAuth()) throw new AppCommonException("メール権限エラー");
  28. } else if ($param instanceof BaseEmailer) {
  29. $this->model = $param->makeModel();
  30. }
  31. }
  32. public function checkAuth()
  33. {
  34. return true;
  35. }
  36. public function getEmailId()
  37. {
  38. $this->model->setId();
  39. return $this->model->id;
  40. }
  41. public function getTimestamp(): Carbon|null
  42. {
  43. return $this->model->updated_at;
  44. }
  45. public function create()
  46. {
  47. $this->model->save();
  48. return [];
  49. }
  50. public function setSubject(string $subject)
  51. {
  52. if ($this->canSend()) {
  53. $this->model->subject = $subject;
  54. } else {
  55. throw new AppCommonException("送信済みメール編集エラー");
  56. }
  57. return $this;
  58. }
  59. public function setContent(string $content)
  60. {
  61. if ($this->canSend()) {
  62. $this->model->content = $content;
  63. } else {
  64. throw new AppCommonException("送信済みメール編集エラー");
  65. }
  66. return $this;
  67. }
  68. public function attach(string $filepath, string $filename, string $mimeType)
  69. {
  70. if ($this->attachments === null) {
  71. $this->attachments = new Collection();
  72. }
  73. $attachment = new EmailAttachment();
  74. $attachment->filepath = $filepath;
  75. $attachment->send_filename = $filename;
  76. $attachment->mime = $mimeType;
  77. $this->attachments->push($attachment);
  78. return $this;
  79. }
  80. public function update()
  81. {
  82. $this->model->save();
  83. return [];
  84. }
  85. public function confirm()
  86. {
  87. if (app()->environment([EnvironmentName::LOCAL->value])) {
  88. $email = $this->model->email;
  89. if (!Str::endsWith($email, ['@sute.jp', '@satellite-tech.co.jp'])) {
  90. info(sprintf("ローカル環境Email送信対象外アドレスのため、メール送信スキップ [%s]", $email));
  91. return;
  92. }
  93. }
  94. $validator = Validator::make(['email' => $this->model->email], [
  95. 'email' => ['required', 'email:strict,dns']
  96. ]);
  97. if ($validator->fails()) {
  98. throw new Exception(sprintf("%s [%s]", $validator->errors()->first(), $this->model->email));
  99. }
  100. if ($this->canSend() !== null) {
  101. $this->model->setId();
  102. ConfirmEvent::dispatch($this->model, $this->attachments);
  103. } else {
  104. throw new AppCommonException("送信済みエラー");
  105. }
  106. }
  107. public function canSend()
  108. {
  109. return $this->canSend;
  110. }
  111. }