Add modular layered architecture with Customers module
Introduce a modular monolith under app/Modules with Domain, Application, Infrastructure and Presentation layers. - Shared module: DomainException base, TransactionManager and an EventDispatcher that publishes events after commit. - Customers module: Customer entity, CustomerName and BusinessId (Y-tunnus checksum) value objects, RegisterCustomer/UpdateCustomer use cases, CustomerQueries read contract, Eloquent repository, and Livewire list and form pages with Finnish translations. - Pest arch tests enforce layer and module dependency rules for every module; Larastan (level 6) added as a dev dependency. - Raise Flux error text contrast to meet WCAG 2.2 AA. - Document the architecture in docs/architecture.md and CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Customers\Domain\Contracts;
|
||||
|
||||
use App\Modules\Customers\Domain\Entities\Customer;
|
||||
use App\Modules\Customers\Domain\ValueObjects\BusinessId;
|
||||
|
||||
interface CustomerRepository
|
||||
{
|
||||
public function findById(int $id): ?Customer;
|
||||
|
||||
/**
|
||||
* Inserts a new customer or updates a stored one, and returns the
|
||||
* stored customer (a new one gets its id here).
|
||||
*/
|
||||
public function save(Customer $customer): Customer;
|
||||
|
||||
public function isBusinessIdTaken(BusinessId $businessId, ?int $exceptCustomerId = null): bool;
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Customers\Domain\Entities;
|
||||
|
||||
use App\Modules\Customers\Domain\Enums\CustomerType;
|
||||
use App\Modules\Customers\Domain\Exceptions\ConsumerCannotHaveBusinessId;
|
||||
use App\Modules\Customers\Domain\ValueObjects\BusinessId;
|
||||
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
|
||||
|
||||
/**
|
||||
* A customer of the company: a business or a private consumer.
|
||||
*
|
||||
* Only the name is needed at job intake; other details are added later.
|
||||
*/
|
||||
final class Customer
|
||||
{
|
||||
private function __construct(
|
||||
private ?int $id,
|
||||
private CustomerName $name,
|
||||
private CustomerType $type,
|
||||
private ?BusinessId $businessId,
|
||||
) {
|
||||
self::ensureBusinessIdFitsType($type, $businessId);
|
||||
}
|
||||
|
||||
/**
|
||||
* A new customer that is not stored yet, so it has no id.
|
||||
*/
|
||||
public static function register(CustomerName $name, CustomerType $type, ?BusinessId $businessId = null): self
|
||||
{
|
||||
return new self(null, $name, $type, $businessId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuilds a stored customer. Used only by repositories.
|
||||
*/
|
||||
public static function reconstitute(int $id, CustomerName $name, CustomerType $type, ?BusinessId $businessId): self
|
||||
{
|
||||
return new self($id, $name, $type, $businessId);
|
||||
}
|
||||
|
||||
public function changeDetails(CustomerName $name, CustomerType $type, ?BusinessId $businessId): void
|
||||
{
|
||||
self::ensureBusinessIdFitsType($type, $businessId);
|
||||
|
||||
$this->name = $name;
|
||||
$this->type = $type;
|
||||
$this->businessId = $businessId;
|
||||
}
|
||||
|
||||
public function id(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function name(): CustomerName
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function type(): CustomerType
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function businessId(): ?BusinessId
|
||||
{
|
||||
return $this->businessId;
|
||||
}
|
||||
|
||||
private static function ensureBusinessIdFitsType(CustomerType $type, ?BusinessId $businessId): void
|
||||
{
|
||||
if ($type === CustomerType::Consumer && $businessId !== null) {
|
||||
throw new ConsumerCannotHaveBusinessId;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Customers\Domain\Enums;
|
||||
|
||||
enum CustomerType: string
|
||||
{
|
||||
case Company = 'company';
|
||||
case Consumer = 'consumer';
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Customers\Domain\Events;
|
||||
|
||||
final readonly class CustomerRegistered
|
||||
{
|
||||
public function __construct(public int $customerId) {}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Customers\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Customers\Domain\ValueObjects\BusinessId;
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class BusinessIdAlreadyInUse extends DomainException
|
||||
{
|
||||
public function __construct(public readonly BusinessId $businessId)
|
||||
{
|
||||
parent::__construct("Business ID {$businessId->value} already belongs to another customer.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Customers\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class ConsumerCannotHaveBusinessId extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('A consumer customer cannot have a business ID.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Customers\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class CustomerNotFound extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $customerId)
|
||||
{
|
||||
parent::__construct("Customer {$customerId} was not found.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Customers\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class InvalidBusinessId extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The business ID is not a valid Finnish business ID.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Customers\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class InvalidCustomerName extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The customer name must be 1–255 characters.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Customers\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Customers\Domain\Exceptions\InvalidBusinessId;
|
||||
|
||||
/**
|
||||
* Finnish business ID (Y-tunnus), e.g. 0737546-2.
|
||||
*
|
||||
* Seven digits, a hyphen and a check digit. Old six-digit IDs are
|
||||
* accepted and padded with a leading zero.
|
||||
*/
|
||||
final readonly class BusinessId
|
||||
{
|
||||
private const array WEIGHTS = [7, 9, 10, 5, 8, 4, 2];
|
||||
|
||||
public string $value;
|
||||
|
||||
public function __construct(string $value)
|
||||
{
|
||||
$value = trim($value);
|
||||
|
||||
if (preg_match('/^(\d{6,7})-(\d)$/', $value, $matches) !== 1) {
|
||||
throw new InvalidBusinessId;
|
||||
}
|
||||
|
||||
$digits = str_pad($matches[1], 7, '0', STR_PAD_LEFT);
|
||||
|
||||
if (self::checkDigitFor($digits) !== (int) $matches[2]) {
|
||||
throw new InvalidBusinessId;
|
||||
}
|
||||
|
||||
$this->value = "{$digits}-{$matches[2]}";
|
||||
}
|
||||
|
||||
public function equals(self $other): bool
|
||||
{
|
||||
return $this->value === $other->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns null when no valid check digit exists (remainder 1).
|
||||
*/
|
||||
private static function checkDigitFor(string $digits): ?int
|
||||
{
|
||||
$sum = 0;
|
||||
|
||||
foreach (str_split($digits) as $index => $digit) {
|
||||
$sum += (int) $digit * self::WEIGHTS[$index];
|
||||
}
|
||||
|
||||
$remainder = $sum % 11;
|
||||
|
||||
return match ($remainder) {
|
||||
0 => 0,
|
||||
1 => null,
|
||||
default => 11 - $remainder,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Customers\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Customers\Domain\Exceptions\InvalidCustomerName;
|
||||
|
||||
final readonly class CustomerName
|
||||
{
|
||||
public const int MAX_LENGTH = 255;
|
||||
|
||||
public string $value;
|
||||
|
||||
public function __construct(string $value)
|
||||
{
|
||||
$value = trim(preg_replace('/\s+/u', ' ', $value) ?? $value);
|
||||
|
||||
if ($value === '' || mb_strlen($value) > self::MAX_LENGTH) {
|
||||
throw new InvalidCustomerName;
|
||||
}
|
||||
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
public function equals(self $other): bool
|
||||
{
|
||||
return $this->value === $other->value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user