Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
112 / 112 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| BootstrapApiDispatcherFactory | |
100.00% |
111 / 111 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
110 / 110 |
|
100.00% |
1 / 1 |
2 | |||
| 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\Configurator; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Core\Api\Infrastructure\Repository\SqlApiKeyRepository; |
| 12 | use App\Core\Api\Infrastructure\Repository\SqlApiLoggerRepository; |
| 13 | use App\Core\Api\Middleware\ApiLoggingMiddleware; |
| 14 | use App\Core\Api\Middleware\ApiPayloadGuardMiddleware; |
| 15 | use App\Core\Api\Middleware\ApiTokenMiddleware; |
| 16 | use App\Core\Api\Middleware\CorsMiddleware; |
| 17 | use App\Core\Api\Middleware\IdempotencyMiddleware; |
| 18 | use App\Core\Api\Middleware\RequestIdMiddleware; |
| 19 | use App\Core\Api\Presentation\ApiDispatcher; |
| 20 | use App\Core\Engine\Application\Security\PermissionContextFactory; |
| 21 | use App\Core\Engine\Presentation\Api\CentralEngineApiController; |
| 22 | use App\Core\Api\Domain\Service\PayloadSanitizer; |
| 23 | use App\Core\Security\Middleware\RateLimiterMiddleware; |
| 24 | use App\Core\Settings\SqlSettingsRepository; |
| 25 | use Nyholm\Psr7\Factory\Psr17Factory; |
| 26 | use PDO; |
| 27 | use Yiisoft\Session\SessionInterface; |
| 28 | use Yiisoft\User\CurrentUser; |
| 29 | |
| 30 | /** |
| 31 | * Factory responsible for assembling the ApiDispatcher and its middleware pipeline. |
| 32 | */ |
| 33 | final readonly class BootstrapApiDispatcherFactory |
| 34 | { |
| 35 | public function __construct( |
| 36 | private Psr17Factory $psr17Factory, |
| 37 | private CurrentUser $currentUser, |
| 38 | private SessionInterface $session, |
| 39 | private CentralEngineApiController $centralEngineApiController |
| 40 | ) { |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Instantiates configured API Dispatcher middleware pipeline. |
| 45 | * |
| 46 | * @param array<string, mixed> $apis Extra API controllers. |
| 47 | */ |
| 48 | public function create( |
| 49 | array $apis, |
| 50 | TranslationAndMenuConfigurator $i18n, |
| 51 | BootstrapEngineFactory $engineFactory, |
| 52 | SecurityAndTemplateConfigurator $sec, |
| 53 | mixed $cacheHandler, |
| 54 | string $prefix, |
| 55 | PDO $pdo |
| 56 | ): ApiDispatcher { |
| 57 | $contextFactory = new PermissionContextFactory($this->session, $pdo); |
| 58 | $apiLogger = new SqlApiLoggerRepository($pdo); |
| 59 | $payloadSanitizer = new PayloadSanitizer(); |
| 60 | $apiLoggingMiddleware = new ApiLoggingMiddleware($apiLogger, $payloadSanitizer); |
| 61 | $activeProfileName = $sec->activeProfile->name; |
| 62 | $apiRateLimit = ($activeProfileName === 'client') ? 60 : 120; |
| 63 | |
| 64 | $exportService = new \App\Core\DataExchange\Application\Service\DataExportService( |
| 65 | $this->centralEngineApiController->getCrudService(), |
| 66 | $pdo |
| 67 | ); |
| 68 | $importService = new \App\Core\DataExchange\Application\Service\DataImportService( |
| 69 | $this->centralEngineApiController->getCrudService(), |
| 70 | $pdo |
| 71 | ); |
| 72 | $dataExchangeApiController = new \App\Core\DataExchange\Presentation\Api\DataExchangeApiController( |
| 73 | $exportService, |
| 74 | $importService, |
| 75 | $this->psr17Factory, |
| 76 | $pdo |
| 77 | ); |
| 78 | |
| 79 | $moduleBuilderService = new \App\Core\ModuleBuilder\Application\Service\ModuleBuilderService( |
| 80 | $pdo, |
| 81 | null, |
| 82 | $prefix |
| 83 | ); |
| 84 | $moduleBuilderApiController = new \App\Core\ModuleBuilder\Presentation\Api\ModuleBuilderApiController( |
| 85 | $moduleBuilderService, |
| 86 | $this->psr17Factory |
| 87 | ); |
| 88 | |
| 89 | return new ApiDispatcher( |
| 90 | $apis['auth'], |
| 91 | $apis['locale'], |
| 92 | $i18n->menuApiController, |
| 93 | $apis['licenses'], |
| 94 | $apis['requirements'], |
| 95 | $this->centralEngineApiController, |
| 96 | $engineFactory->gridWidgetApiController, |
| 97 | $engineFactory->picklistValuesApiController, |
| 98 | $i18n->translationApiController, |
| 99 | $i18n->settingsApiController, |
| 100 | $i18n->layoutApiController, |
| 101 | $i18n->cronApiController, |
| 102 | $apis['mail'], |
| 103 | $sec->auditApiController, |
| 104 | $contextFactory, |
| 105 | new RequestIdMiddleware(), |
| 106 | new CorsMiddleware($this->psr17Factory), |
| 107 | new ApiPayloadGuardMiddleware($this->psr17Factory), |
| 108 | RateLimiterMiddleware::createWithCache( |
| 109 | $this->psr17Factory, |
| 110 | $cacheHandler, |
| 111 | 10, |
| 112 | 60, |
| 113 | $sec->securityAuditLogger |
| 114 | ), |
| 115 | RateLimiterMiddleware::createWithCache( |
| 116 | $this->psr17Factory, |
| 117 | $cacheHandler, |
| 118 | $apiRateLimit, |
| 119 | 60, |
| 120 | $sec->securityAuditLogger |
| 121 | ), |
| 122 | new IdempotencyMiddleware($this->psr17Factory, $cacheHandler), |
| 123 | $sec->csrfMiddleware, |
| 124 | new ApiTokenMiddleware( |
| 125 | $this->psr17Factory, |
| 126 | new SqlSettingsRepository($i18n->settingsApiController, $prefix), |
| 127 | new SqlApiKeyRepository($pdo, $prefix), |
| 128 | $sec->securityAuditLogger, |
| 129 | $this->currentUser, |
| 130 | $this->session |
| 131 | ), |
| 132 | $this->psr17Factory, |
| 133 | $apis['instance'] ?? null, |
| 134 | $apiLoggingMiddleware, |
| 135 | $apis['preferences'] ?? null, |
| 136 | $apis['profile'] ?? null, |
| 137 | $apis['dav_device'] ?? null, |
| 138 | $apis['webmail'] ?? null, |
| 139 | $apis['webmail_events'] ?? null, |
| 140 | $engineFactory->workflowApiController, |
| 141 | $apis['mfa'] ?? null, |
| 142 | $engineFactory->projectGanttApiController, |
| 143 | $engineFactory->kanbanApiController, |
| 144 | $engineFactory->documentApiController, |
| 145 | $apis['maps'] ?? null, |
| 146 | $apis['pdf'] ?? null, |
| 147 | $activeProfileName, |
| 148 | $apis['ownership'] ?? null, |
| 149 | $apis['structure'] ?? null, |
| 150 | $apis['access'] ?? null, |
| 151 | $apis['profile_permissions'] ?? null, |
| 152 | $engineFactory->mediaUploadApiController, |
| 153 | $apis['search'] ?? null, |
| 154 | $apis['search_settings'] ?? null, |
| 155 | $apis['comments'] ?? null, |
| 156 | $apis['dashboard'] ?? null, |
| 157 | $apis['tax_rates'] ?? null, |
| 158 | $apis['tax_groups'] ?? null, |
| 159 | $apis['tax_rules'] ?? null, |
| 160 | $apis['tax_calculate'] ?? null, |
| 161 | $apis['discounts'] ?? null, |
| 162 | $apis['inventory_fields'] ?? null, |
| 163 | $apis['currencies'] ?? null, |
| 164 | $apis['inventory_items'] ?? null, |
| 165 | $engineFactory->languageTranslationsApiController, |
| 166 | $dataExchangeApiController, |
| 167 | $moduleBuilderApiController, |
| 168 | $engineFactory->gridWidgetManagementApiController |
| 169 | ); |
| 170 | } |
| 171 | } |