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.

110 lines
3.1KB

  1. <?php
  2. namespace App\Transmission;
  3. use App\Models\Htpms\MstCustomer;
  4. use App\Models\HtpmsCustomer\TblAdjuster;
  5. use App\Models\HtpmsCustomer\TblPark;
  6. use App\Models\HtpmsSystem\SmstServerSystem;
  7. use App\Transmission\Layouts\IFRequest;
  8. use Carbon\Carbon;
  9. use Illuminate\Http\Client\Response;
  10. use Illuminate\Support\Facades\Http;
  11. class IFSender extends Sender
  12. {
  13. private string $host = 'localhost';
  14. private int $port = 80;
  15. private string $path = 'cgi-bin/HT';
  16. private $customerCode;
  17. private $parkingManagementCode;
  18. private TblPark $park;
  19. private TblAdjuster $adjuster;
  20. private SmstServerSystem $system;
  21. private MstCustomer $customer;
  22. private bool $already_init = false;
  23. public function __construct(string $customerCode, string $parkingManagementCode)
  24. {
  25. $this->customerCode = $customerCode;
  26. $this->parkingManagementCode = $parkingManagementCode;
  27. }
  28. public function init(string $host = null, int $port = null, string $path = null)
  29. {
  30. if ($host === null || $port === null || $path === null) {
  31. $this->setModels();
  32. }
  33. $this->setHost($host);
  34. $this->setPort($port);
  35. $this->setPath($path);
  36. $this->already_init = true;
  37. }
  38. public function send(IFRequest $request): Response
  39. {
  40. if (!$this->already_init) {
  41. $this->init();
  42. }
  43. $now = Carbon::now();
  44. $request->header->customerCode = $this->customerCode;
  45. $request->header->parkingManagementCode = $this->parkingManagementCode;
  46. $request->header->resultCode = ResultCode::SUCCESS->value;
  47. $request->header->sendDatetime = $now->format('YmdHis');
  48. $data = $request->toArray();
  49. $url = $this->getUrl();
  50. info('SEND ' . $request->header->interfaceId, [$data, $url]);
  51. $this->response = Http::post($url, $data);
  52. return $this->response;
  53. }
  54. private function setModels(): void
  55. {
  56. $this->customer = MstCustomer::customerCode($this->customerCode)->firstOrFail();
  57. $this->system = SmstServerSystem::firstOrFail();
  58. TblPark::setCustomerId($this->customer->id);
  59. $this->park = TblPark::parkingManagementCode($this->parkingManagementCode)->firstOrFail();
  60. $this->adjuster = TblAdjuster::isUpdateMachine($this->park->id)
  61. ->firstOrFail();
  62. }
  63. private function setHost(string|null $host): void
  64. {
  65. if ($host !== null) {
  66. $this->host = $host;
  67. } else {
  68. $this->host = $this->adjuster->adjuster_ipaddress;
  69. }
  70. }
  71. private function setPort(int|null $port): void
  72. {
  73. if ($port !== null) {
  74. $this->port = $port;
  75. } else {
  76. $this->port = $this->system->port_number_season_ticket_update;
  77. }
  78. }
  79. private function setPath(string|null $path): void
  80. {
  81. if ($path !== null) {
  82. $this->path = $path;
  83. } else {
  84. // デフォルト値のまま
  85. }
  86. }
  87. public function getUrl(): string
  88. {
  89. return 'http://' . $this->host . ':' . $this->port . '/' . $this->path;
  90. }
  91. }