Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
MapWebController
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 actionIndex
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
5
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\Modules\Map\Presentation\Web;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Modules\Map\Domain\Repository\MapApiClientInterface;
12use App\Modules\Map\Infrastructure\Provider\SmartMapLoadBalancer;
13use Psr\Http\Message\ResponseFactoryInterface;
14use Psr\Http\Message\ResponseInterface;
15use Psr\Http\Message\ServerRequestInterface;
16use Twig\Environment as TwigEnvironment;
17use Yiisoft\User\CurrentUser;
18
19/**
20 * Map Full-Page Web Controller.
21 *
22 * Serves the primary /maps enterprise GIS interface: Tabler UI integration,
23 * address searching, point dropping, hierarchy visualization, and route planning.
24 *
25 * @package App\Modules\Map\Presentation\Web
26 */
27final readonly class MapWebController
28{
29    /**
30     * MapWebController constructor.
31     *
32     * @param TwigEnvironment           $twig            Twig template engine.
33     * @param CurrentUser               $currentUser     Authenticated identity service.
34     * @param ResponseFactoryInterface  $responseFactory PSR-17 response factory.
35     * @param MapApiClientInterface     $mapClient       Ammonly Map API client.
36     * @param SmartMapLoadBalancer|null $loadBalancer    Map load balancer.
37     */
38    public function __construct(
39        private TwigEnvironment $twig,
40        private CurrentUser $currentUser,
41        private ResponseFactoryInterface $responseFactory,
42        private MapApiClientInterface $mapClient,
43        private ?SmartMapLoadBalancer $loadBalancer = null
44    ) {
45    }
46
47    /**
48     * Handles GET /maps.
49     *
50     * @param ServerRequestInterface $request PSR-7 Server request.
51     * @return ResponseInterface Rendered HTML response.
52     */
53    public function actionIndex(ServerRequestInterface $request): ResponseInterface
54    {
55        $params = $request->getQueryParams();
56        $targetModule = isset($params['module']) ? (string) $params['module'] : '';
57        $targetId = isset($params['id']) ? (int) $params['id'] : 0;
58        $batchIds = isset($params['ids']) && is_string($params['ids'])
59            ? array_filter(array_map('intval', explode(',', $params['ids'])))
60            : [];
61
62        $maptiler = $this->loadBalancer?->getProvider('maptiler');
63        $html = $this->twig->render('modules/map/index.twig', [
64            'page_title'             => 'Ammonly Maps',
65            'tile_url_template'      => $this->mapClient->getTileUrlTemplate(),
66            'tile_dark_url_template' => $this->mapClient->getDarkTileUrlTemplate(),
67            'tile_maptiler_url'      => $maptiler?->getTileUrlTemplate(false) ?? '',
68            'tile_dark_maptiler_url' => $maptiler?->getTileUrlTemplate(true) ?? '',
69            'api_base_url'           => $this->mapClient->getApiUrl(),
70            'api_key'                => $this->mapClient->getApiKey(),
71            'current_user'           => $this->currentUser,
72            'target_module'          => $targetModule,
73            'target_id'              => $targetId,
74            'batch_ids'              => $batchIds,
75        ]);
76
77        $response = $this->responseFactory->createResponse(200);
78        $response->getBody()->write($html);
79
80        return $response->withHeader('Content-Type', 'text/html; charset=utf-8');
81    }
82}