Files
gorilla/app/Service/Service.php
2026-01-12 12:37:16 +01:00

82 lines
2.3 KiB
PHP

<?php
namespace App\Service;
use App\Service\Entities\FailureReport;
use App\Service\Entities\InputEntity;
use App\Service\Entities\Inspection;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
class Service
{
private array $unprocessable = [];
private array $types = [Inspection::class, FailureReport::class];
private array $entities = [];
private array $descriptions = [];
public function processEntry(array $rawEntry)
{
try {
$inputEntity = new InputEntity($rawEntry);
} catch (\Exception $e) {
$this->unprocessable[] = $rawEntry;
throw $e;
}
if(in_array(trim(mb_strtolower($inputEntity->description)), $this->descriptions)) {
$this->unprocessable[] = $rawEntry;
throw new \InvalidArgumentException('Duplicate description');
}
$entity = $this->create($inputEntity);
Log::channel('parser')->info('Created entity ', [
'number' => $inputEntity->number,
'type' => $entity->type->value,
]);
if($entity) {
$this->entities[] = $entity;
$this->descriptions[] = trim(mb_strtolower($inputEntity->description));
return true;
}
$this->unprocessable[] = $rawEntry;
return false;
}
public function getUnprocessable(): array {
return $this->unprocessable;
}
public function getInspections(): array {
return $this->getEntities(Inspection::class)->map->__toArray()->all();
}
public function getInspectionsCount(): int {
return $this->getEntities(Inspection::class)->count();
}
public function getFailureReports(): array {
return $this->getEntities(FailureReport::class)->map->__toArray()->all();;
}
public function getFailureReportsCount(): int
{
return $this->getEntities(FailureReport::class)->count();
}
private function getEntities($type): Collection
{
return collect($this->entities)
->whereInstanceOf($type)
->values();
}
private function create(InputEntity $inputEntity)
{
foreach ($this->types as $type) {
if($type::isValid($inputEntity)) {
return new $type($inputEntity);
}
}
return null;
}
}