Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.65% |
59 / 63 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| MapModuleServiceProvider | |
93.55% |
58 / 62 |
|
50.00% |
1 / 2 |
2.00 | |
0.00% |
0 / 1 |
| getDefinitions | |
93.44% |
57 / 61 |
|
0.00% |
0 / 1 |
1.00 | |||
| getExtensions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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\Map\Config; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Modules\Map\Application\Service\IpGeolocationService; |
| 12 | use App\Modules\Map\Application\Service\MapGeocodingService; |
| 13 | use App\Modules\Map\Application\Service\MapRoutingService; |
| 14 | use App\Modules\Map\Domain\Repository\IpGeolocationServiceInterface; |
| 15 | use App\Modules\Map\Domain\Repository\MapApiClientInterface; |
| 16 | use App\Modules\Map\Infrastructure\Client\AmmonlyMapApiClient; |
| 17 | use App\Modules\Map\Infrastructure\GeoIp\MaxMindMmdbReader; |
| 18 | use App\Modules\Map\Infrastructure\Provider\AmmonlyLocalMapProvider; |
| 19 | use App\Modules\Map\Infrastructure\Provider\MapTilerMapProvider; |
| 20 | use App\Modules\Map\Infrastructure\Provider\SmartMapLoadBalancer; |
| 21 | use App\Modules\Map\Presentation\Api\MapApiController; |
| 22 | use App\Modules\Map\Presentation\Htmx\MapHtmxController; |
| 23 | use App\Modules\Map\Presentation\Web\MapWebController; |
| 24 | use App\Shared\Infrastructure\Config\InstallerConfigLoader; |
| 25 | use Nyholm\Psr7\Factory\Psr17Factory; |
| 26 | use PDO; |
| 27 | use Twig\Environment as TwigEnvironment; |
| 28 | use Yiisoft\Di\ServiceProviderInterface; |
| 29 | use Yiisoft\User\CurrentUser; |
| 30 | |
| 31 | /** |
| 32 | * MapModuleServiceProvider. |
| 33 | * |
| 34 | * Configures DI container definitions for Ammonly Maps Engine client, services, and controllers. |
| 35 | * |
| 36 | * @package App\Modules\Map\Config |
| 37 | */ |
| 38 | final class MapModuleServiceProvider implements ServiceProviderInterface |
| 39 | { |
| 40 | /** |
| 41 | * @return array<string, mixed> Definitions mapping for PSR-11 container. |
| 42 | */ |
| 43 | public function getDefinitions(): array |
| 44 | { |
| 45 | return [ |
| 46 | MapApiClientInterface::class => static function ( |
| 47 | ?PDO $pdo = null |
| 48 | ): MapApiClientInterface { |
| 49 | $config = InstallerConfigLoader::load(); |
| 50 | $apiUrl = (string) ($config['maps']['api_url'] ?? $_ENV['AMMONLY_MAP_API_URL'] |
| 51 | ?? 'https://map.ammonly.com'); |
| 52 | $apiKey = (string) ($config['maps']['api_key'] ?? $_ENV['AMMONLY_MAP_API_KEY'] ?? ''); |
| 53 | |
| 54 | return new AmmonlyMapApiClient($apiUrl, $apiKey, null, $pdo); |
| 55 | }, |
| 56 | |
| 57 | MaxMindMmdbReader::class => static function (): MaxMindMmdbReader { |
| 58 | $config = InstallerConfigLoader::load(); |
| 59 | $dbPath = (string) ($config['maps']['geoip_db_path'] |
| 60 | ?? dirname(__DIR__, 4) . '/resources/geoip/GeoLite2-City.mmdb'); |
| 61 | |
| 62 | return new MaxMindMmdbReader($dbPath); |
| 63 | }, |
| 64 | |
| 65 | IpGeolocationServiceInterface::class => static fn( |
| 66 | PDO $pdo, |
| 67 | MaxMindMmdbReader $reader |
| 68 | ): IpGeolocationServiceInterface => new IpGeolocationService($pdo, $reader), |
| 69 | |
| 70 | SmartMapLoadBalancer::class => static function ( |
| 71 | MapApiClientInterface $client |
| 72 | ): SmartMapLoadBalancer { |
| 73 | $config = InstallerConfigLoader::load(); |
| 74 | $maptilerKey = (string) ($config['maps']['maptiler_key'] ?? $_ENV['MAPTILER_API_KEY'] |
| 75 | ?? MapTilerMapProvider::DEFAULT_KEY); |
| 76 | |
| 77 | return new SmartMapLoadBalancer([ |
| 78 | new AmmonlyLocalMapProvider($client), |
| 79 | new MapTilerMapProvider($maptilerKey), |
| 80 | ]); |
| 81 | }, |
| 82 | |
| 83 | MapGeocodingService::class => static fn( |
| 84 | MapApiClientInterface $client |
| 85 | ): MapGeocodingService => new MapGeocodingService($client), |
| 86 | |
| 87 | MapRoutingService::class => static fn( |
| 88 | MapApiClientInterface $client |
| 89 | ): MapRoutingService => new MapRoutingService($client), |
| 90 | |
| 91 | MapWebController::class => static fn( |
| 92 | TwigEnvironment $twig, |
| 93 | CurrentUser $user, |
| 94 | Psr17Factory $psr17, |
| 95 | MapApiClientInterface $client, |
| 96 | SmartMapLoadBalancer $balancer |
| 97 | ): MapWebController => new MapWebController($twig, $user, $psr17, $client, $balancer), |
| 98 | |
| 99 | MapApiController::class => static fn( |
| 100 | Psr17Factory $psr17, |
| 101 | MapGeocodingService $geo, |
| 102 | MapRoutingService $routing, |
| 103 | PDO $pdo, |
| 104 | IpGeolocationServiceInterface $ipGeo, |
| 105 | SmartMapLoadBalancer $balancer |
| 106 | ): MapApiController => new MapApiController($psr17, $geo, $routing, $pdo, $ipGeo, $balancer), |
| 107 | |
| 108 | MapHtmxController::class => static fn( |
| 109 | TwigEnvironment $twig, |
| 110 | Psr17Factory $psr17, |
| 111 | MapApiClientInterface $client, |
| 112 | PDO $pdo, |
| 113 | IpGeolocationServiceInterface $ipGeo, |
| 114 | SmartMapLoadBalancer $balancer |
| 115 | ): MapHtmxController => new MapHtmxController($twig, $psr17, $client, $pdo, $ipGeo, $balancer), |
| 116 | ]; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @return array<string, mixed> Extensions mapping. |
| 121 | */ |
| 122 | public function getExtensions(): array |
| 123 | { |
| 124 | return []; |
| 125 | } |
| 126 | } |