Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.62% covered (success)
93.62%
44 / 47
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserAndAuthBootstrapProvider
93.48% covered (success)
93.48%
43 / 46
50.00% covered (danger)
50.00%
1 / 2
10.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 initialize
93.33% covered (success)
93.33%
42 / 45
0.00% covered (danger)
0.00%
0 / 1
9.02
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Bootstrap\Provider;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Core\Bootstrap\Configurator\SecurityAndTemplateConfigurator;
12use App\Core\Engine\Infrastructure\Repository\SqlAuditRepository;
13use App\Core\Engine\Infrastructure\Settings\EngineSettings;
14use App\Core\Event\SystemEventDispatcher;
15use App\Core\Time\NtpTimeSyncService;
16use App\Modules\Automation\Application\Listener\WorkflowRecordLifecycleListener;
17use App\Modules\Automation\Application\Service\WorkflowDagEngine;
18use App\Modules\Automation\Infrastructure\Repository\SqlWorkflowRepository;
19use App\Modules\Mail\Application\Service\SystemMailSenderService;
20use App\Modules\User\Application\Service\PasswordResetService;
21use App\Modules\User\Application\Service\UserMfaDeviceService;
22use App\Modules\User\Infrastructure\Repository\SqlUserRepository;
23use App\Modules\User\Infrastructure\Repository\UserIdentityRepository;
24use App\Modules\User\Presentation\Api\AuthApiController;
25use App\Modules\User\Presentation\Api\MfaApiController;
26use App\Modules\User\Presentation\Web\AuthController;
27use Nyholm\Psr7\Factory\Psr17Factory;
28use PDO;
29use Yiisoft\Session\SessionInterface;
30use Yiisoft\User\CurrentUser;
31
32/**
33 * Bootstrap provider encapsulating user repositories, authentication, MFA, and event dispatchers.
34 */
35final 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}