Feature: Creating Privacy Basics
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Privacy;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Modules\Core\Privacy\Contracts\PersonalDataProvider;
|
||||
|
||||
/**
|
||||
* The registry every PersonalDataProvider is collected through. A module registers
|
||||
* by adding its provider's class name to config('core.privacy.providers') — the
|
||||
* same shape as Lunar's own config('lunar.search.indexers') model->indexer map, just
|
||||
* a plain list since a provider isn't keyed to one model. Core never references a
|
||||
* specific provider class; a future ERP/banking/etc. module just adds its own
|
||||
* provider class to that config array and PrivacyService picks it up automatically.
|
||||
*/
|
||||
class PrivacyManager
|
||||
{
|
||||
public function __construct(private readonly Container $container) {}
|
||||
|
||||
/**
|
||||
* @return array<int, PersonalDataProvider>
|
||||
*/
|
||||
public function providers(): array
|
||||
{
|
||||
$providers = array_map(
|
||||
fn (string $class) => $this->container->make($class),
|
||||
config('core.privacy.providers', [])
|
||||
);
|
||||
|
||||
$this->assertUniqueNames($providers);
|
||||
|
||||
return $providers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, PersonalDataProvider> $providers
|
||||
*/
|
||||
private function assertUniqueNames(array $providers): void
|
||||
{
|
||||
$names = array_map(fn (PersonalDataProvider $provider) => $provider->name(), $providers);
|
||||
$duplicates = array_diff_assoc($names, array_unique($names));
|
||||
|
||||
if ($duplicates !== []) {
|
||||
throw new \LogicException(
|
||||
'Duplicate Modules\Core\Privacy provider name(s): '.implode(', ', array_unique($duplicates))
|
||||
.'. Each provider registered in config(\'core.privacy.providers\') must return a unique name().'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user