Skip to content

Commit 99edd61

Browse files
committed
UHF-12838: Allow configuring mobilenote lookback offset
1 parent 0b45745 commit 99edd61

8 files changed

Lines changed: 53 additions & 50 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sync_lookback_offset: '-30 days'

public/modules/custom/helfi_kymp_content/config/schema/helfi_kymp_content.schema.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,18 @@ helfi_kymp_content.project_search:
66
project_search_path:
77
type: path
88
label: 'Project search path'
9+
10+
helfi_kymp_content.settings:
11+
type: config_object
12+
label: 'Mobile note'
13+
mapping:
14+
address_api_key:
15+
type: string
16+
wfs_url:
17+
type: string
18+
wfs_username:
19+
type: string
20+
wfs_password:
21+
type: string
22+
sync_lookback_offset:
23+
type: string

public/modules/custom/helfi_kymp_content/src/MobileNoteDataService.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Drupal\helfi_kymp_content;
66

77
use Drupal\Component\Datetime\TimeInterface;
8-
use Drupal\Core\Site\Settings;
8+
use Drupal\Core\Config\ConfigFactoryInterface;
99
use Drupal\Core\TypedData\Exception\MissingDataException;
1010
use Drupal\Core\TypedData\TypedDataManagerInterface;
1111
use Drupal\Core\Utility\Error;
@@ -49,7 +49,7 @@ public function __construct(
4949
protected ClientInterface $client,
5050
protected TypedDataManagerInterface $typedDataManager,
5151
protected TimeInterface $time,
52-
protected Settings $settings,
52+
protected ConfigFactoryInterface $configFactory,
5353
protected PaikkatietoClient $paikkatietoClient,
5454
) {
5555
// EPSG:3879 is Helsinki local CRS (ETRS-GK25FIN).
@@ -136,20 +136,20 @@ public function fetchNearbyStreets(array $items): void {
136136
* @throws \InvalidArgumentException
137137
*/
138138
protected function fetchFromApi(): array {
139-
$settings = $this->settings->get('helfi_kymp_mobilenote', []);
139+
$settings = $this->configFactory->get('helfi_kymp_content.settings');
140140

141141
if (
142-
empty($settings['wfs_url']) ||
143-
empty($settings['wfs_username']) ||
144-
empty($settings['wfs_password'])
142+
empty($settings->get('wfs_url')) ||
143+
empty($settings->get('wfs_username')) ||
144+
empty($settings->get('wfs_password'))
145145
) {
146146
throw new \InvalidArgumentException('MobileNote: Missing API credentials.');
147147
}
148148

149149
try {
150150
$minDate = (new \DateTime())
151151
->setTimestamp($this->time->getRequestTime())
152-
->modify($settings['sync_lookback_offset'] ?? '-30 days');
152+
->modify($settings->get('sync_lookback_offset') ?? '-30 days');
153153
}
154154
catch (\DateMalformedStringException $e) {
155155
throw new \InvalidArgumentException($e->getMessage(), previous: $e);
@@ -166,16 +166,16 @@ protected function fetchFromApi(): array {
166166
</Filter>
167167
XML;
168168

169-
$response = $this->client->request('GET', $settings['wfs_url'], [
170-
'auth' => [$settings['wfs_username'], $settings['wfs_password']],
169+
$response = $this->client->request('GET', $settings->get('wfs_url'), [
170+
'auth' => [$settings->get('wfs_username'), $settings->get('wfs_password')],
171171
'query' => [
172172
'service' => 'WFS',
173173
'version' => '1.1.0',
174174
'request' => 'GetFeature',
175175
'typeName' => 'ppoytakirjaExtranet',
176176
'outputFormat' => 'application/json',
177177
'srsName' => 'EPSG:3879',
178-
'maxFeatures' => 1000,
178+
'maxFeatures' => 10000,
179179
'filter' => preg_replace('/\s+/', ' ', trim($filterXml)),
180180
],
181181
'timeout' => 30,

public/modules/custom/helfi_kymp_content/src/Paikkatieto/PaikkatietoClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Drupal\helfi_kymp_content\Paikkatieto;
66

7-
use Drupal\Core\Site\Settings;
7+
use Drupal\Core\Config\ConfigFactoryInterface;
88
use GuzzleHttp\ClientInterface;
99
use GuzzleHttp\Exception\GuzzleException;
1010
use GuzzleHttp\Exception\RequestException;
@@ -27,7 +27,7 @@ class PaikkatietoClient implements LoggerAwareInterface {
2727
private const float EARTH_RADIUS = 6371000;
2828

2929
public function __construct(
30-
private readonly Settings $settings,
30+
private readonly ConfigFactoryInterface $configFactory,
3131
private readonly ClientInterface $httpClient,
3232
) {
3333
}
@@ -142,8 +142,8 @@ public function fetchStreetsByPoint(float $lat, float $lon, int $distance = 75):
142142
* @throws \InvalidArgumentException
143143
*/
144144
private function makeRequest(array $queryParams, int $maxRetries = 3): array {
145-
$apiSettings = $this->settings->get('helfi_kymp_mobilenote', []);
146-
$apiKey = $apiSettings['address_api_key'] ?? NULL;
145+
$config = $this->configFactory->get('helfi_kymp_content.settings');
146+
$apiKey = $config->get('address_api_key');
147147

148148
if (empty($apiKey)) {
149149
throw new \InvalidArgumentException('Paikkatietoapi: Missing API key.');

public/modules/custom/helfi_kymp_content/tests/src/Kernel/MobileNoteCronHooksTest.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Drupal\Component\Datetime\TimeInterface;
88
use Drupal\Core\Entity\EntityStorageInterface;
99
use Drupal\Core\Entity\EntityTypeManagerInterface;
10-
use Drupal\Core\Site\Settings;
1110
use Drupal\Core\State\StateInterface;
1211
use Drupal\helfi_kymp_content\Hook\MobileNoteCronHooks;
1312
use Drupal\helfi_kymp_content\MobileNoteDataService;
@@ -62,13 +61,11 @@ protected function setUp(): void {
6261
parent::setUp();
6362

6463
// Configure MobileNote API settings.
65-
$settings = Settings::getAll();
66-
$settings['helfi_kymp_mobilenote'] = [
67-
'wfs_url' => 'https://example.com/wfs',
68-
'wfs_username' => 'user',
69-
'wfs_password' => 'pass',
70-
];
71-
new Settings($settings);
64+
$this->config('helfi_kymp_content.settings')
65+
->set('wfs_url', 'https://example.com/wfs')
66+
->set('wfs_username', 'user')
67+
->set('wfs_password', 'pass')
68+
->save();
7269

7370
// Mock HTTP client with fixture data.
7471
$client = $this->createMockHttpClient([

public/modules/custom/helfi_kymp_content/tests/src/Kernel/MobileNoteDataServiceTest.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Drupal\Tests\helfi_kymp_content\Kernel;
66

7-
use Drupal\Core\Site\Settings;
87
use Drupal\helfi_kymp_content\MobileNoteDataService;
98
use Drupal\KernelTests\KernelTestBase;
109
use GuzzleHttp\ClientInterface;
@@ -44,16 +43,13 @@ class MobileNoteDataServiceTest extends KernelTestBase {
4443
protected function setUp(): void {
4544
parent::setUp();
4645

47-
// Mock settings.
48-
$settings = Settings::getAll();
49-
$settings['helfi_kymp_mobilenote'] = [
50-
'wfs_url' => 'https://example.com/wfs',
51-
'wfs_username' => 'test_user',
52-
'wfs_password' => 'test_pass',
53-
'sync_lookback_offset' => '-30 days',
54-
'address_api_key' => 'TEST_KEY',
55-
];
56-
new Settings($settings);
46+
$this->config('helfi_kymp_content.settings')
47+
->set('wfs_url', 'https://example.com/wfs')
48+
->set('wfs_username', 'test_user')
49+
->set('wfs_password', 'test_pass')
50+
->set('sync_lookback_offset', '-30 days')
51+
->set('address_api_key', 'TEST_KEY')
52+
->save();
5753
}
5854

5955
/**

public/modules/custom/helfi_kymp_content/tests/src/Kernel/MobileNoteDataSourceTest.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Drupal\Tests\helfi_kymp_content\Kernel;
66

7-
use Drupal\Core\Site\Settings;
87
use Drupal\helfi_kymp_content\Plugin\DataType\MobileNoteData;
98
use Drupal\KernelTests\KernelTestBase;
109
use Drupal\search_api\Entity\Index;
@@ -35,14 +34,12 @@ class MobileNoteDataSourceTest extends KernelTestBase {
3534
* Tests data fetching and coordinate conversion.
3635
*/
3736
public function testDatasource(): void {
38-
new Settings([
39-
'helfi_kymp_mobilenote' => [
40-
'wfs_url' => 'https://example.com/wfs',
41-
'wfs_username' => 'test_user',
42-
'wfs_password' => 'test_pass',
43-
'sync_lookback_offset' => '-30 days',
44-
],
45-
]);
37+
$this->config('helfi_kymp_content.settings')
38+
->set('wfs_url', 'https://example.com/wfs')
39+
->set('wfs_username', 'test_user')
40+
->set('wfs_password', 'test_pass')
41+
->set('sync_lookback_offset', '-30 days')
42+
->save();
4643

4744
$this->setupMockHttpClient([
4845
new Response(body: json_encode([

public/sites/default/all.settings.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,9 @@
7474
$config['raven.settings']['ignored_channels'][] = 'search_api';
7575

7676
// MobileNote WFS API settings.
77-
$settings['helfi_kymp_mobilenote'] = [
78-
'wfs_url' => getenv('MN_WFS_URL') ?: '',
79-
'wfs_username' => getenv('MN_WFS_USERNAME') ?: '',
80-
'wfs_password' => getenv('MN_WFS_PASSWORD') ?: '',
81-
'address_api_key' => getenv('PAIKKATIETOHAKU_API_KEY') ?: '',
82-
// Sync filter: fetch items where voimassaoloAlku >= (today - offset).
83-
'sync_lookback_offset' => '-30 days',
84-
];
77+
$config['helfi_kymp_content.settings']['wfs_url'] = getenv('MN_WFS_URL');
78+
$config['helfi_kymp_content.settings']['wfs_username'] = getenv('MN_WFS_USERNAME');
79+
$config['helfi_kymp_content.settings']['wfs_password'] = getenv('MN_WFS_PASSWORD');
8580

81+
// Paikkatietohaku:
82+
$config['helfi_kymp_content.settings']['address_api_key'] = getenv('PAIKKATIETOHAKU_API_KEY');

0 commit comments

Comments
 (0)