create cmms machine service with tests

This commit is contained in:
2024-12-08 10:27:14 +03:30
parent 822b2ae526
commit c8e024981e
11 changed files with 420 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Console\Commands;
use App\Models\CMMSMachine;
use App\Services\CMMS\GetMachinesInfoListService;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use SoapClient;
class GetMachinesInfoListCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cmms_machines_info:get_list';
/**
* The console command description.
*
* @var string
*/
protected $description = 'receive cmms cars information list and update the corresponding tables';
/**
* Execute the console command.
* @throws Exception
*/
public function handle(GetMachinesInfoListService $getMachinesInfoListService): void
{
$response = $getMachinesInfoListService->run();
$updatedRows = 0;
if (isset($response['vw_MachBankFull'])) {
foreach ($response['vw_MachBankFull'] as $entry) {
// Insert data into the database
CMMSMachine::query()->updateOrCreate(
[
'car_id' => $entry['Id'],
'machine_code' => $entry['MachineCode'],
],
[
'car_name' => $entry['CarName'] ?? null,
'car_group' => $entry['CarGroup'],
'machine_code' => $entry['MachineCode'],
'unit_name' => $entry['UnitName'],
'car_status' => $entry['Status'],
'car_id' => $entry['Id'],
'unit_group' => $entry['UnitGroup'],
'plak_number' => $entry['PlakNumber'] ?? null,
]);
$updatedRows += 1;
}
}
$this->info("{$updatedRows} row's updated successfully");
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CMMSMachine extends Model
{
use HasFactory;
protected $table = 'cmms_machines';
protected $guarded = [];
}

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Services\CMMS;
use Exception;
use Illuminate\Support\Facades\Log;
use SoapClient;
class GetMachinesInfoListService
{
protected string $url;
protected string $channelName;
protected string $password;
protected string $viewName;
public function __construct()
{
$this->url = config('cmms_web_services.MACHINE_INFO.url');
$this->password = config('cmms_web_services.MACHINE_INFO.password');
$this->viewName = config('cmms_web_services.MACHINE_INFO.ViewName');
$this->channelName = 'cmms_machines_info';
ini_set('memory_limit', '512M');
}
/**
* @throws Exception
*/
public function run(): array
{
try {
$xmlResponse = $this->sendRequest();
return $this->xmlToArray($xmlResponse);
}
catch (Exception $e) {
Log::channel($this->channelName)
->error(get_class($this),
[
'message' => $e->getMessage(),
]
);
throw $e;
}
}
public function sendRequest()
{
$client = new SoapClient($this->url);
return $client->GetView([
'ViewName' => $this->viewName,
'Password' => $this->password,
])->GetViewResult;
}
public function xmlToArray($xmlString)
{
$xmlObject = simplexml_load_string($xmlString);
$json = json_encode($xmlObject);
return json_decode($json, true);
}
}