領収証発行サービス
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.0KB

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