|
- <?php
-
- namespace App\Http\Controllers\Web\Email;
-
- use App\Codes\Email;
- use App\Email\BaseEmailer;
- use App\Email\Members\EntryApprove;
- use App\Email\Members\EntryPaymentComplete;
- use App\Email\Members\TerminateOrderApprove;
- use App\Email\Members\UserInfoUpdateOrderApprove;
- use App\Email\Members\VehicleInfoUpdateOrderApprove;
- use App\Http\Controllers\Web\WebController;
- use App\Kintone\Models\SeasonTicketContract;
- use App\Kintone\Models\SeasonTicketContractEntry;
- use App\Kintone\Models\TerminateApplication;
- use App\Kintone\Models\UserInfoUpdateApplication;
- use App\Kintone\Models\VehicleInfoUpdateApplication;
- use App\Logic\EmailManager;
- use Exception;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Request;
- use LogicException;
-
- class EmailSendController extends WebController
- {
- private BaseEmailer|null $email = null;
-
- private EmailManager|null $emailManager = null;
-
- public function name(): string
- {
- return "メール送信依頼";
- }
-
- public function description(): string
- {
- return "メール送信依頼を登録する";
- }
-
-
- public function __construct(protected EmailSendParam $param)
- {
- parent::__construct();
- }
-
- protected function run(Request $request): JsonResponse
- {
- try {
- // トークンチェック
- $this->checkToken();
-
- // メール作成
- $this->getEmail();
-
- // 送信
- if ($this->emailManager === null) {
- throw new LogicException("EmailManager不正");
- }
- $this->emailManager->confirm();
- } catch (Exception $e) {
- logger($e->getMessage());
- logger($e->getFile());
- logger($e->getLine());
- return $this->failed();
- }
-
- return $this->success();
- }
-
- private function success()
- {
- return response()->json([
- 'result' => 'SUCCESS'
- ]);
- }
-
- private function failed()
- {
- return response()->json([
- 'result' => 'FAILED'
- ]);
- }
-
- private function checkToken()
- {
-
- $currectToken = config('mail.emailSendOrderToken');
-
- if ($this->param->token !== $currectToken) {
- logs()->warning("Email送信依頼トークン不正");
- throw new Exception("Email送信依頼トークン不正");
- }
- }
-
- private function getEmail()
- {
- $emailId = $this->param->emailId;
-
- if ($emailId === Email::TERMINATE_ORDER_APPROVE) {
- $application = TerminateApplication::findByApplicationNo($this->param->applicationNo);
- $seasonTicketContract = SeasonTicketContract::find($application->seasonTicketContractRecordNo);
- $this->setEmail(new TerminateOrderApprove($seasonTicketContract, $application));
- return;
- }
- if ($emailId === Email::VEHICLE_INFO_UPDATE_ORDER_APPROVE) {
- $application = VehicleInfoUpdateApplication::findByApplicationNo($this->param->applicationNo);
- $seasonTicketContract = SeasonTicketContract::find($application->seasonTicketContractRecordNo);
- $this->setEmail(new VehicleInfoUpdateOrderApprove($seasonTicketContract, $application));
- return;
- }
- if ($emailId === Email::USER_INFO_UPDATE_ORDER_APPROVE) {
- $application = UserInfoUpdateApplication::findByApplicationNo($this->param->applicationNo);
- $seasonTicketContract = SeasonTicketContract::find($application->seasonTicketContractRecordNo);
- $this->setEmail(new UserInfoUpdateOrderApprove($seasonTicketContract, $application));
- return;
- }
- if ($emailId === Email::ENTRY_APPROVE) {
- $entry = SeasonTicketContractEntry::find($this->param->seasonTicketContractEntryRecordNo);
- $parking = $entry->getParking();
- $this->setEmail(new EntryApprove($parking, $entry));
- return;
- }
- if ($emailId === Email::ENTRY_PAYMENT_COMPLETE) {
- $entry = SeasonTicketContractEntry::find($this->param->seasonTicketContractEntryRecordNo);
- $parking = $entry->getParking();
- $this->setEmail(new EntryPaymentComplete($parking, $entry));
- return;
- }
-
-
- if ($this->email === null || $this->emailManager === null) {
- throw new LogicException("setEmail不正");
- }
- }
-
- private function setEmail(BaseEmailer $email)
- {
- $this->email = $email;
- $this->emailManager = new EmailManager($email);
- }
- }
|