領収証発行サービス
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

74 linhas
1.6KB

  1. <?php
  2. namespace App\Logic\ReceiptIssuingOrder;
  3. use App\Exceptions\AppCommonException;
  4. use App\Features\LoginUser;
  5. use App\Models\ReceiptIssuingOrder;
  6. use App\Util\DateUtil;
  7. use Illuminate\Support\Str;
  8. use LogicException;
  9. abstract class ReceiptIssuingOrderManager
  10. {
  11. use LoginUser;
  12. protected bool $initialized = false;
  13. protected function __construct(protected ReceiptIssuingOrder $order)
  14. {
  15. }
  16. protected function fetch(string $receiptIssuingOrderId)
  17. {
  18. $order = ReceiptIssuingOrder::findOrFail($receiptIssuingOrderId);
  19. if (!$this->loginUser()->checkAuthorization($order)) {
  20. throw new AppCommonException("認可不良");
  21. }
  22. $this->order = $order;
  23. }
  24. protected function refreshToken()
  25. {
  26. $order = $this->order;
  27. $order->access_token = base64_encode(Str::uuid());
  28. $order->access_token_expires_at = DateUtil::now()->adddays(7);
  29. }
  30. protected function checkToken(string $token): bool
  31. {
  32. if ($this->order->isNotSavedModel()) {
  33. throw new LogicException("初期化不良");
  34. }
  35. $order = ReceiptIssuingOrder::whereAccessToken($token)
  36. ->first();
  37. if (!($order instanceof ReceiptIssuingOrder)) {
  38. return false;
  39. }
  40. if ($order->access_token_expires_at === null) {
  41. return false;
  42. }
  43. if ($order->access_token !== $token) {
  44. return false;
  45. }
  46. $now = DateUtil::now();
  47. $ret = $now->lt($this->order->access_token_expires_at);
  48. if (!$ret) {
  49. return false;
  50. }
  51. $this->order = $order;
  52. return true;
  53. }
  54. }