49 lines
1.6 KiB
PHP
49 lines
1.6 KiB
PHP
<?php
|
||
|
||
namespace App\Communication;
|
||
|
||
use App\Enums\Medium;
|
||
use App\Enums\MessageEffect;
|
||
use App\Enums\MessageIntent;
|
||
use App\Support\Settings;
|
||
|
||
/**
|
||
* The medium × intent policy (V2.1): `config/communication.php` is the ceiling, the landlord's `communication_rules`
|
||
* (tenants) may only narrow it: switch a medium off, withdraw effects. Widening is ignored.
|
||
*/
|
||
class CommunicationRules
|
||
{
|
||
public function mediumActive(Medium $medium): bool
|
||
{
|
||
$ceiling = (bool) config("communication.media.{$medium->value}.active", false);
|
||
|
||
return $ceiling && ($this->narrowing()['media'][$medium->value]['active'] ?? true) !== false;
|
||
}
|
||
|
||
public function consentRequired(Medium $medium): bool
|
||
{
|
||
return (bool) config("communication.media.{$medium->value}.consent_required", false);
|
||
}
|
||
|
||
/**
|
||
* @return list<MessageEffect> the effects the rules allow for the intent on the medium (never empty: review at least)
|
||
*/
|
||
public function effects(Medium $medium, MessageIntent $intent): array
|
||
{
|
||
/** @var list<MessageEffect> $ceiling */
|
||
$ceiling = config('communication.effects.'.$intent->value, [MessageEffect::Review]);
|
||
$withdrawn = (array) ($this->narrowing()['effects'][$medium->value][$intent->value] ?? []);
|
||
$allowed = array_values(array_filter($ceiling, fn (MessageEffect $effect): bool => ! in_array($effect->value, $withdrawn, true)));
|
||
|
||
return $allowed === [] ? [MessageEffect::Review] : $allowed;
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function narrowing(): array
|
||
{
|
||
return (array) (Settings::tenant()?->communication_rules ?? []);
|
||
}
|
||
}
|