Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
30 / 30 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| DataMappingWebController | |
100.00% |
29 / 29 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
3 | |||
| 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\Modules\DataMapping\Presentation\Web; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Core\Engine\Application\Service\DataMappingApplicationService; |
| 12 | use App\Core\Engine\Domain\Repository\MetadataRepositoryInterface; |
| 13 | use App\Core\Instance\Application\Service\InstanceContextManagerInterface; |
| 14 | use Nyholm\Psr7\Factory\Psr17Factory; |
| 15 | use Psr\Http\Message\ResponseInterface; |
| 16 | use Psr\Http\Message\ServerRequestInterface; |
| 17 | use Twig\Environment as TwigEnvironment; |
| 18 | |
| 19 | /** |
| 20 | * Web controller for Data Mapping configuration and management interface. |
| 21 | */ |
| 22 | final readonly class DataMappingWebController |
| 23 | { |
| 24 | private const string HTML_CONTENT_TYPE = 'text/html; charset=utf-8'; |
| 25 | |
| 26 | public function __construct( |
| 27 | private DataMappingApplicationService $mappingService, |
| 28 | private MetadataRepositoryInterface $metadataRepository, |
| 29 | private TwigEnvironment $twig, |
| 30 | private Psr17Factory $psr17Factory, |
| 31 | private ?InstanceContextManagerInterface $instanceManager = null, |
| 32 | private string $appProfile = 'admin', |
| 33 | ) { |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Renders data mapping settings index view. |
| 38 | */ |
| 39 | public function index(): ResponseInterface |
| 40 | { |
| 41 | $modules = $this->metadataRepository->findAllActiveModules(); |
| 42 | $mappings = $this->mappingService->getAllMappings(); |
| 43 | |
| 44 | $modulesMap = []; |
| 45 | foreach ($modules as $module) { |
| 46 | $modulesMap[$module->id] = $module; |
| 47 | } |
| 48 | |
| 49 | $enrichedMappings = []; |
| 50 | foreach ($mappings as $mapping) { |
| 51 | $sourceId = (int) ($mapping['source_module_id'] ?? 0); |
| 52 | $targetId = (int) ($mapping['target_module_id'] ?? 0); |
| 53 | $enrichedMappings[] = [ |
| 54 | 'id' => $mapping['id'] ?? 0, |
| 55 | 'source_module_id' => $sourceId, |
| 56 | 'target_module_id' => $targetId, |
| 57 | 'source_module' => $modulesMap[$sourceId] ?? null, |
| 58 | 'target_module' => $modulesMap[$targetId] ?? null, |
| 59 | 'mapping_name' => $mapping['mapping_name'] ?? 'Mapping', |
| 60 | 'is_active' => $mapping['is_active'] ?? 1, |
| 61 | ]; |
| 62 | } |
| 63 | |
| 64 | $html = $this->twig->render('data_mapping/index.twig', [ |
| 65 | 'app_profile' => $this->appProfile, |
| 66 | 'active_instance' => $this->instanceManager?->getActiveInstance(), |
| 67 | 'modules' => $modules, |
| 68 | 'mappings' => $enrichedMappings, |
| 69 | ]); |
| 70 | |
| 71 | $response = $this->psr17Factory->createResponse(200) |
| 72 | ->withHeader('Content-Type', self::HTML_CONTENT_TYPE); |
| 73 | $response->getBody()->write($html); |
| 74 | |
| 75 | return $response; |
| 76 | } |
| 77 | } |