|
- <?php
-
- namespace App\Transmission;
-
- use App\Models\Htpms\MstCustomer;
- use App\Models\HtpmsCustomer\TblAdjuster;
- use App\Models\HtpmsCustomer\TblPark;
- use App\Models\HtpmsSystem\SmstServerSystem;
- use App\Transmission\Layouts\IFRequest;
- use Carbon\Carbon;
- use Illuminate\Http\Client\Response;
- use Illuminate\Support\Facades\Http;
-
- class IFSender extends Sender
- {
- private string $host = 'localhost';
- private int $port = 80;
- private string $path = 'cgi-bin/HT';
-
- private $customerCode;
- private $parkingManagementCode;
-
- private TblPark $park;
- private TblAdjuster $adjuster;
- private SmstServerSystem $system;
- private MstCustomer $customer;
-
- private bool $already_init = false;
-
- public function __construct(string $customerCode, string $parkingManagementCode)
- {
- $this->customerCode = $customerCode;
- $this->parkingManagementCode = $parkingManagementCode;
- }
-
- public function init(string $host = null, int $port = null, string $path = null)
- {
- if ($host === null || $port === null || $path === null) {
- $this->setModels();
- }
- $this->setHost($host);
- $this->setPort($port);
- $this->setPath($path);
-
- $this->already_init = true;
- }
-
- public function send(IFRequest $request): Response
- {
- if (!$this->already_init) {
- $this->init();
- }
-
- $now = Carbon::now();
-
- $request->header->customerCode = $this->customerCode;
- $request->header->parkingManagementCode = $this->parkingManagementCode;
- $request->header->resultCode = ResultCode::SUCCESS->value;
- $request->header->sendDatetime = $now->format('YmdHis');
- $data = $request->toArray();
-
- $url = $this->getUrl();
- info('SEND ' . $request->header->interfaceId, [$data, $url]);
-
- $this->response = Http::post($url, $data);
- return $this->response;
- }
-
-
- private function setModels(): void
- {
- $this->customer = MstCustomer::customerCode($this->customerCode)->firstOrFail();
- $this->system = SmstServerSystem::firstOrFail();
- TblPark::setCustomerId($this->customer->id);
- $this->park = TblPark::parkingManagementCode($this->parkingManagementCode)->firstOrFail();
- $this->adjuster = TblAdjuster::isUpdateMachine($this->park->id)
- ->firstOrFail();
- }
-
- private function setHost(string|null $host): void
- {
- if ($host !== null) {
- $this->host = $host;
- } else {
- $this->host = $this->adjuster->adjuster_ipaddress;
- }
- }
- private function setPort(int|null $port): void
- {
- if ($port !== null) {
- $this->port = $port;
- } else {
- $this->port = $this->system->port_number_season_ticket_update;
- }
- }
- private function setPath(string|null $path): void
- {
- if ($path !== null) {
- $this->path = $path;
- } else {
- // デフォルト値のまま
- }
- }
-
- public function getUrl(): string
- {
- return 'http://' . $this->host . ':' . $this->port . '/' . $this->path;
- }
- }
|