163 lines
4.3 KiB
PHP
163 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Machines\Domain\Entities;
|
|
|
|
use App\Modules\Machines\Domain\Enums\MachineType;
|
|
use App\Modules\Machines\Domain\Exceptions\MachineInactive;
|
|
use App\Modules\Machines\Domain\Exceptions\MachineNeedsServiceUnit;
|
|
use App\Modules\Machines\Domain\ValueObjects\MachineCounter;
|
|
use App\Modules\Machines\Domain\ValueObjects\MachineName;
|
|
|
|
/**
|
|
* A machine (kone, spec 19.4): a printing press, a sticker cutter or other,
|
|
* in one or more service units, with a responsible person who gets the
|
|
* maintenance notifications, and counters. Machines are never deleted: one
|
|
* out of use is not offered, but its history stays.
|
|
*/
|
|
final class Machine
|
|
{
|
|
/**
|
|
* @param list<int> $serviceUnitIds
|
|
* @param list<MachineCounter> $counters
|
|
*/
|
|
private function __construct(
|
|
private ?int $id,
|
|
private MachineName $name,
|
|
private MachineType $type,
|
|
private ?string $description,
|
|
private array $serviceUnitIds,
|
|
private ?int $responsibleUserId,
|
|
private array $counters,
|
|
private bool $active,
|
|
) {
|
|
if ($serviceUnitIds === []) {
|
|
throw new MachineNeedsServiceUnit;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $serviceUnitIds
|
|
* @param list<MachineCounter> $counters
|
|
*
|
|
* @throws MachineNeedsServiceUnit
|
|
*/
|
|
public static function create(MachineName $name, MachineType $type, ?string $description, array $serviceUnitIds, ?int $responsibleUserId, array $counters): self
|
|
{
|
|
return new self(null, $name, $type, self::text($description), array_values(array_unique($serviceUnitIds)), $responsibleUserId, $counters, true);
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $serviceUnitIds
|
|
* @param list<MachineCounter> $counters
|
|
*/
|
|
public static function reconstitute(int $id, MachineName $name, MachineType $type, ?string $description, array $serviceUnitIds, ?int $responsibleUserId, array $counters, bool $active): self
|
|
{
|
|
return new self($id, $name, $type, $description, $serviceUnitIds, $responsibleUserId, $counters, $active);
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $serviceUnitIds
|
|
* @param list<MachineCounter> $counters
|
|
*
|
|
* @throws MachineNeedsServiceUnit
|
|
*/
|
|
public function change(MachineName $name, MachineType $type, ?string $description, array $serviceUnitIds, ?int $responsibleUserId, array $counters): void
|
|
{
|
|
if ($serviceUnitIds === []) {
|
|
throw new MachineNeedsServiceUnit;
|
|
}
|
|
|
|
$this->name = $name;
|
|
$this->type = $type;
|
|
$this->description = self::text($description);
|
|
$this->serviceUnitIds = array_values(array_unique($serviceUnitIds));
|
|
$this->responsibleUserId = $responsibleUserId;
|
|
$this->counters = $counters;
|
|
}
|
|
|
|
/**
|
|
* Whether the machine serves any of the service units.
|
|
*
|
|
* @param list<int> $serviceUnitIds
|
|
*/
|
|
public function servesAnyOf(array $serviceUnitIds): bool
|
|
{
|
|
return array_intersect($this->serviceUnitIds, $serviceUnitIds) !== [];
|
|
}
|
|
|
|
/**
|
|
* @throws MachineInactive
|
|
*/
|
|
public function ensureActive(): void
|
|
{
|
|
if (! $this->active) {
|
|
throw new MachineInactive;
|
|
}
|
|
}
|
|
|
|
public function activate(): void
|
|
{
|
|
$this->active = true;
|
|
}
|
|
|
|
public function deactivate(): void
|
|
{
|
|
$this->active = false;
|
|
}
|
|
|
|
public function id(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function name(): MachineName
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
public function type(): MachineType
|
|
{
|
|
return $this->type;
|
|
}
|
|
|
|
public function description(): ?string
|
|
{
|
|
return $this->description;
|
|
}
|
|
|
|
/**
|
|
* @return list<int>
|
|
*/
|
|
public function serviceUnitIds(): array
|
|
{
|
|
return $this->serviceUnitIds;
|
|
}
|
|
|
|
public function responsibleUserId(): ?int
|
|
{
|
|
return $this->responsibleUserId;
|
|
}
|
|
|
|
/**
|
|
* @return list<MachineCounter>
|
|
*/
|
|
public function counters(): array
|
|
{
|
|
return $this->counters;
|
|
}
|
|
|
|
public function isActive(): bool
|
|
{
|
|
return $this->active;
|
|
}
|
|
|
|
private static function text(?string $value): ?string
|
|
{
|
|
$value = $value === null ? '' : trim($value);
|
|
|
|
return $value === '' ? null : mb_substr($value, 0, 5000);
|
|
}
|
|
}
|