init
This commit is contained in:
19
app/Service/Entities/BaseReport.php
Normal file
19
app/Service/Entities/BaseReport.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Entities;
|
||||
|
||||
use App\Service\Enums\Status;
|
||||
use App\Service\Enums\Type;
|
||||
use Carbon\Carbon;
|
||||
use DateTimeImmutable;
|
||||
|
||||
abstract class BaseReport
|
||||
{
|
||||
public Type $type;
|
||||
public string $description = '';
|
||||
public ?Carbon $serviceDate = null;
|
||||
public Status $status = Status::NEW;
|
||||
public string $serviceNotes = '';
|
||||
public ?string $contactPhone = null;
|
||||
public DateTimeImmutable $createdAt;
|
||||
}
|
||||
9
app/Service/Entities/EntityInterface.php
Normal file
9
app/Service/Entities/EntityInterface.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Entities;
|
||||
|
||||
interface EntityInterface
|
||||
{
|
||||
public function __construct(InputEntity $entity);
|
||||
public static function isValid(InputEntity $entity): bool;
|
||||
}
|
||||
66
app/Service/Entities/FailureReport.php
Normal file
66
app/Service/Entities/FailureReport.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Entities;
|
||||
|
||||
use App\Service\Enums\Priority;
|
||||
use App\Service\Enums\Status;
|
||||
use App\Service\Enums\Type;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class FailureReport extends BaseReport implements EntityInterface
|
||||
{
|
||||
public Priority $priority = Priority::NORMAL;
|
||||
public string $serviceNotes = '';
|
||||
public Type $type = Type::FAILURE;
|
||||
|
||||
|
||||
public function __construct(InputEntity $entity)
|
||||
{
|
||||
$this->createdAt = new DateTimeImmutable();
|
||||
$this->fromData($entity);
|
||||
}
|
||||
|
||||
public static function isValid(InputEntity $entity): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function __toArray(): array
|
||||
{
|
||||
return [
|
||||
'description' => $this->description,
|
||||
'type' => $this->type->value,
|
||||
'priority' => $this->priority->value,
|
||||
'serviceDate' => $this->serviceDate?->format('Y-m-d') ?? '',
|
||||
'status' => $this->status->value,
|
||||
'serviceNotes' => $this->serviceNotes,
|
||||
'contactPhone' => $this->contactPhone ?? '',
|
||||
'createdAt' => $this->createdAt->format('Y-m-d')
|
||||
];
|
||||
}
|
||||
|
||||
private function fromData(InputEntity $entity): void
|
||||
{
|
||||
$this->description = $entity->description;
|
||||
$this->serviceDate = $entity->date;
|
||||
$this->contactPhone = $entity->phone;
|
||||
$this->checkPriority($entity->description);
|
||||
$this->status = $this->serviceDate ? Status::DEADLINE : Status::NEW;
|
||||
|
||||
}
|
||||
|
||||
private function checkPriority(string $description): void
|
||||
{
|
||||
$map = [
|
||||
'/bardzo pilne/im' => Priority::CRITICAL,
|
||||
'/pilne/im' => Priority::HIGH,
|
||||
];
|
||||
|
||||
foreach ($map as $pattern => $priority) {
|
||||
if (preg_match($pattern, $description)) {
|
||||
$this->priority = $priority;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
60
app/Service/Entities/InputEntity.php
Normal file
60
app/Service/Entities/InputEntity.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Entities;
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
class InputEntity
|
||||
{
|
||||
public int $number;
|
||||
public string $description;
|
||||
public ?Carbon $date = null;
|
||||
public ?string $phone = null;
|
||||
|
||||
public function __construct(private readonly array $data) {
|
||||
$this->parse();
|
||||
}
|
||||
|
||||
private function parse()
|
||||
{
|
||||
$this->validateNumber();
|
||||
$this->validateDescription();
|
||||
$this->validateDate();
|
||||
$this->validatePhone();
|
||||
|
||||
}
|
||||
|
||||
private function validateNumber(): void {
|
||||
if(!array_key_exists('number', $this->data) || !preg_match('/^\d+$/', trim($this->data['number']))) throw new \InvalidArgumentException('Invalid number');
|
||||
$this->number = (int)$this->data['number'];
|
||||
}
|
||||
|
||||
private function validateDescription(): void {
|
||||
if(!array_key_exists('description', $this->data) || empty(trim($this->data['description']))) throw new \InvalidArgumentException('Empty description');
|
||||
$this->description = trim($this->data['description']);
|
||||
}
|
||||
|
||||
private function validateDate(): void {
|
||||
if(!array_key_exists('dueDate', $this->data)) return;
|
||||
$date = trim((string)$this->data['dueDate']);
|
||||
if(!strlen($date)) return;
|
||||
if (preg_match('/^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$/', $date)) {
|
||||
$this->date = Carbon::parse($date);
|
||||
return;
|
||||
}
|
||||
throw new \InvalidArgumentException('Invalid date');
|
||||
}
|
||||
|
||||
private function validatePhone(): void {
|
||||
if(!array_key_exists('phone', $this->data)) return;
|
||||
$rawPhone = $this->data['phone'];
|
||||
$phone = trim((string)$rawPhone);
|
||||
if(!strlen($phone)) return;
|
||||
$checkPhone = str_replace([' ', '-'], '', $phone);
|
||||
if(!preg_match('/^\+?\d{9,12}$/', $checkPhone)) {
|
||||
throw new \InvalidArgumentException('Invalid phone number');
|
||||
}
|
||||
$this->phone = $rawPhone;
|
||||
}
|
||||
}
|
||||
57
app/Service/Entities/Inspection.php
Normal file
57
app/Service/Entities/Inspection.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Entities;
|
||||
|
||||
use App\Service\Enums\Status;
|
||||
use App\Service\Enums\Type;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class Inspection extends BaseReport implements EntityInterface
|
||||
{
|
||||
|
||||
public ?int $serviceWeek = null;
|
||||
public Type $type = Type::INSPECTION;
|
||||
|
||||
public function __construct(InputEntity $entity)
|
||||
{
|
||||
$this->createdAt = new DateTimeImmutable();
|
||||
$this->fromData($entity);
|
||||
}
|
||||
|
||||
public static function isValid(InputEntity $entity): bool
|
||||
{
|
||||
return preg_match('/przegląd/mi', $entity->description);
|
||||
}
|
||||
|
||||
public function __toArray(): array
|
||||
{
|
||||
return [
|
||||
'description' => $this->description,
|
||||
'type' => $this->type->value,
|
||||
'inspectionDate' => $this->serviceDate?->format('Y-m-d') ?? '',
|
||||
'weekOfInspection' => $this->serviceWeek ?? '',
|
||||
'status' => $this->status->value,
|
||||
'recommendations' => $this->serviceNotes ?? '',
|
||||
'contactPhone' => $this->contactPhone ?? '',
|
||||
'createdAt' => $this->createdAt->format('Y-m-d')
|
||||
];
|
||||
}
|
||||
|
||||
private function fromData(InputEntity $entity): void
|
||||
{
|
||||
$this->description = $entity->description;
|
||||
$this->contactPhone = $entity->phone;
|
||||
$this->serviceDate = $entity->date;
|
||||
$this->checkWeek();
|
||||
$this->status = $this->serviceDate ? Status::PLANED : Status::NEW;
|
||||
}
|
||||
|
||||
private function checkWeek(): void
|
||||
{
|
||||
if($this->serviceDate) {
|
||||
$this->serviceWeek = $this->serviceDate->isoWeek();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user