Contracts and Integrations
A package uses another package only through its contracts and never reads another package's tables.
The public surface of a package
Every module of a package has a Contract namespace. Another package may use:
- the interfaces in
<Module>\, which the module implements, such asContract Employmentof the core orFacts Absenceof the leave package, with their data objects inDays Query Contract\;Dto - the events in
Contract\and the messages inEvent Contract\(see Events and Messages);Message - the ports in
Application\, interfaces another package implements, such as a dashboard widget or a reference type of reports;Port - the value objects in
Domain\and the exceptions inValue Object Exception.
The core's module Shared may be used by all. Everything else, including entities, repositories and services, is
internal and may change in any release. A package uses contracts only of packages listed in require or suggest
of its composer..
use WebVision\Adsum\Core\Employee\Contract\EmploymentFacts;
final readonly class TeamHolidays
{
public function __construct(
private EmploymentFacts $employment,
) {}
public function weeklyHours(string $employeeId, \DateTimeImmutable $date): ?int
{
return $this->employment->on($employeeId, $date)?->weeklyContractSeconds;
}
}
Implementing a contract or a port
The package that declares a contract interface or a port ships its contract test, an abstract class
<Interface>Contract in its tests. The test of a single implementation extends it and returns the implementation:
final class CompanyFoundingWriterTest extends CompanyFoundingContract
{
protected function founding(): CompanyFounding
{
$subject = static::getContainer()->get(CompanyFounding::class);
$this->assertInstanceOf(CompanyWriter::class, $subject);
return $subject;
}
}
A registry, such as the reference types of reports, runs its contract test for every registered implementation of every installed package.
Integrating with a suggested package
Code that works only with another package installed, such as the asset reports for the reporting package, lives in
src/ and is registered in config/. The core loads that
file only where the named package is installed. List that package in suggest:
# config/integrations/web-vision/adsum-reporting.yaml
services:
_defaults:
autowire: true
autoconfigure: true
WebVision\Adsum\Assets\Integration\Reporting\:
resource: '../../../src/Integration/Reporting/'
No other code of the package names the suggested package. Its tests sit in tests/
and are marked #, which skips them where the package is missing.
Relating records to a company or an employee
A record that belongs to a company or an employee maps a relation to the core's contract interfaces
Company and Employee, never to the core's entities, so that it is deleted with the company or
employee:
use Doctrine\ORM\Mapping as ORM;
use WebVision\Adsum\Core\Employee\Contract\EmployeeRecord;
use WebVision\Adsum\Core\Organisation\Contract\CompanyRecord;
#[ORM\Entity]
class Certificate
{
#[ORM\ManyToOne(targetEntity: CompanyRecord::class)]
#[ORM\JoinColumn(name: 'company_id', nullable: false, onDelete: 'CASCADE')]
private CompanyRecord $company;
#[ORM\ManyToOne(targetEntity: EmployeeRecord::class)]
#[ORM\JoinColumn(name: 'employee_id', nullable: false, onDelete: 'CASCADE')]
private EmployeeRecord $employee;
public function __construct(CompanyRecord $company, EmployeeRecord $employee)
{
$this->company = $company;
$this->employee = $employee;
}
public function employeeId(): string
{
return $this->employee->id();
}
}
With only an ID, get the record from Company or Employee (loads nothing).
Queries compare the relation with the ID (c.). The migration adds the foreign keys with
ON DELETE CASCADE:
$this->alterTable('certificate')
->addForeignKeyConstraint('company', ['company_id'], ['id'], ['onDelete' => 'CASCADE'], 'fk_certificate_company')
->addForeignKeyConstraint('employee', ['employee_id'], ['id'], ['onDelete' => 'CASCADE'], 'fk_certificate_employee');
Guards
These architecture tests in the unit suite enforce the rules of this page and name the class that breaks one:
| Test | Fails when |
|---|---|
Package | a package uses an internal class of another, or a package it does not require or suggest |
Table | SQL names a table of another module, or of a suggested package outside src/ |
Contract | a contract or port has no contract test, or an implementation does not run it |
Type | the frontend of a package imports, calls or translates what its package may not use |
Listeners | a class injects or creates an event listener or a message handler |
Async | a message differs from its description, or a field changed without a new version |
Events | an event or a message has no entry in this manual |
Record | an entity maps company_ or employee_ without a relation that deletes with the company or the
employee |