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>\Contract, which the module implements, such as EmploymentFacts of the core or AbsenceDaysQuery of the leave package, with their data objects in Contract\Dto;
  • the events in Contract\Event and the messages in Contract\Message (see Events and Messages);
  • the ports in Application\Port, interfaces another package implements, such as a dashboard widget or a reference type of reports;
  • the value objects in Domain\ValueObject and the exceptions in 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.json.

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;
    }
}
Copied!

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;
    }
}
Copied!

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/Integration/<Package>/ and is registered in config/integrations/<vendor>/<name>.yaml. 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/'
Copied!

No other code of the package names the suggested package. Its tests sit in tests/Functional/Integration/<Package>/ and are marked #[RequiresPackage('vendor/name')], 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 CompanyRecord and EmployeeRecord, 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();
    }
}
Copied!

With only an ID, get the record from CompanyReferences::of() or EmployeeReferences::of() (loads nothing). Queries compare the relation with the ID (c.employee = :employee). 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');
Copied!

Guards 

These architecture tests in the unit suite enforce the rules of this page and name the class that breaks one:

Test Fails when
PackageContractsTest a package uses an internal class of another, or a package it does not require or suggest
TableOwnershipTest SQL names a table of another module, or of a suggested package outside src/Integration/
ContractTestsTest a contract or port has no contract test, or an implementation does not run it
TypeScriptBoundariesTest the frontend of a package imports, calls or translates what its package may not use
ListenersAreNotCalledDirectlyTest a class injects or creates an event listener or a message handler
AsyncApiDocumentsTest a message differs from its description, or a field changed without a new version
EventsAndMessagesDocumentedTest an event or a message has no entry in this manual
RecordRelationsTest an entity maps company_id or employee_id without a relation that deletes with the company or the employee