領収証発行サービス
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

102 řádky
2.3KB

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