Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.62% |
44 / 47 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| UserAndAuthBootstrapProvider | |
93.48% |
43 / 46 |
|
50.00% |
1 / 2 |
10.03 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| initialize | |
93.33% |
42 / 45 |
|
0.00% |
0 / 1 |
9.02 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Core\Bootstrap\Provider; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Core\Bootstrap\Configurator\SecurityAndTemplateConfigurator; |
| 12 | use App\Core\Engine\Infrastructure\Repository\SqlAuditRepository; |
| 13 | use App\Core\Engine\Infrastructure\Settings\EngineSettings; |
| 14 | use App\Core\Event\SystemEventDispatcher; |
| 15 | use App\Core\Time\NtpTimeSyncService; |
| 16 | use App\Modules\Automation\Application\Listener\WorkflowRecordLifecycleListener; |
| 17 | use App\Modules\Automation\Application\Service\WorkflowDagEngine; |
| 18 | use App\Modules\Automation\Infrastructure\Repository\SqlWorkflowRepository; |
| 19 | use App\Modules\Mail\Application\Service\SystemMailSenderService; |
| 20 | use App\Modules\User\Application\Service\PasswordResetService; |
| 21 | use App\Modules\User\Application\Service\UserMfaDeviceService; |
| 22 | use App\Modules\User\Infrastructure\Repository\SqlUserRepository; |
| 23 | use App\Modules\User\Infrastructure\Repository\UserIdentityRepository; |
| 24 | use App\Modules\User\Presentation\Api\AuthApiController; |
| 25 | use App\Modules\User\Presentation\Api\MfaApiController; |
| 26 | use App\Modules\User\Presentation\Web\AuthController; |
| 27 | use Nyholm\Psr7\Factory\Psr17Factory; |
| 28 | use PDO; |
| 29 | use Yiisoft\Session\SessionInterface; |
| 30 | use Yiisoft\User\CurrentUser; |
| 31 | |
| 32 | /** |
| 33 | * Bootstrap provider encapsulating user repositories, authentication, MFA, and event dispatchers. |
| 34 | */ |
| 35 | final readonly class UserAndAuthBootstrapProvider |
| 36 | { |
| 37 | private const string LOGS_SUBPATH = '/storage/logs'; |
| 38 | |
| 39 | public function __construct( |
| 40 | private Psr17Factory $psr17Factory, |
| 41 | private SessionInterface $session |
| 42 | ) { |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Initializes user repository, authentication, MFA, and system event dispatchers. |
| 47 | * |
| 48 | * @return array{ |
| 49 | * 0: AuthApiController, |
| 50 | * 1: ?MfaApiController, |
| 51 | * 2: AuthController, |
| 52 | * 3: SystemEventDispatcher, |
| 53 | * 4: SqlAuditRepository, |
| 54 | * 5: CurrentUser, |
| 55 | * 6: SqlUserRepository |
| 56 | * } |
| 57 | */ |
| 58 | public function initialize( |
| 59 | ?PDO $pdo, |
| 60 | string $prefix, |
| 61 | string $basePath, |
| 62 | SecurityAndTemplateConfigurator $sec |
| 63 | ): array { |
| 64 | $userPrefix = ($sec->activeProfile->name === 'client') ? 'c_' : $prefix; |
| 65 | $userRepository = new SqlUserRepository($pdo, $userPrefix); |
| 66 | $identityRepository = new UserIdentityRepository($userRepository); |
| 67 | |
| 68 | $auditRepo = new SqlAuditRepository($pdo, $prefix); |
| 69 | $engineSettings = new EngineSettings($pdo, $prefix); |
| 70 | $workflowRepo = new SqlWorkflowRepository($pdo, $prefix); |
| 71 | $workflowEngine = new WorkflowDagEngine($workflowRepo, $pdo, $prefix); |
| 72 | $workflowListener = new WorkflowRecordLifecycleListener($workflowEngine); |
| 73 | $eventDispatcher = new SystemEventDispatcher($auditRepo, $engineSettings, null, $workflowListener); |
| 74 | |
| 75 | $currentUser = new CurrentUser($identityRepository, $eventDispatcher); |
| 76 | |
| 77 | $mfaService = $pdo !== null |
| 78 | ? new UserMfaDeviceService($pdo, $userRepository, null, null, $userPrefix) |
| 79 | : null; |
| 80 | $timeService = $pdo !== null ? new NtpTimeSyncService($pdo) : null; |
| 81 | $mailSender = $pdo !== null ? new SystemMailSenderService($pdo) : null; |
| 82 | $passwordResetService = ($pdo !== null && $timeService !== null && $mailSender !== null) |
| 83 | ? new PasswordResetService($pdo, $userRepository, $timeService, $mailSender, $userPrefix) |
| 84 | : null; |
| 85 | |
| 86 | $authApiController = new AuthApiController( |
| 87 | $this->psr17Factory, |
| 88 | $userRepository, |
| 89 | $basePath . self::LOGS_SUBPATH, |
| 90 | null, |
| 91 | $mfaService, |
| 92 | $passwordResetService |
| 93 | ); |
| 94 | $mfaApiController = $mfaService !== null |
| 95 | ? new MfaApiController($this->psr17Factory, $mfaService, $currentUser) |
| 96 | : null; |
| 97 | |
| 98 | $authController = new AuthController( |
| 99 | $this->psr17Factory, |
| 100 | $sec->twig, |
| 101 | $authApiController, |
| 102 | $currentUser, |
| 103 | $this->session |
| 104 | ); |
| 105 | |
| 106 | return [ |
| 107 | $authApiController, |
| 108 | $mfaApiController, |
| 109 | $authController, |
| 110 | $eventDispatcher, |
| 111 | $auditRepo, |
| 112 | $currentUser, |
| 113 | $userRepository, |
| 114 | ]; |
| 115 | } |
| 116 | } |