Skip to content

Commit 7d3a9b6

Browse files
committedDec 12, 2024
[TASK] CSV Helper added for parsing Warncell data
1 parent bdf59cc commit 7d3a9b6

9 files changed

+118
-24
lines changed
 

‎Build/phpunit/UnitTestsBootstrap.php

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
if (!getenv('TYPO3_PATH_ROOT')) {
4848
putenv('TYPO3_PATH_ROOT=' . rtrim($testbase->getWebRoot(), '/'));
4949
}
50+
5051
if (!getenv('TYPO3_PATH_WEB')) {
5152
putenv('TYPO3_PATH_WEB=' . rtrim($testbase->getWebRoot(), '/'));
5253
}

‎Classes/Helper/CsvHelper.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the package jweiland/weather2.
7+
*
8+
* For the full copyright and license information, please read the
9+
* LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace JWeiland\Weather2\Helper;
13+
14+
class CsvHelper
15+
{
16+
/**
17+
* Converts a CSV string into an array of rows.
18+
*
19+
* @param string $csvData The CSV data as a string
20+
* @param string $delimiter The delimiter used in the CSV (default is ',')
21+
* @return array<int, array<int, string>> Array of rows where each row is an array of fields
22+
*/
23+
public function convertCsvToArray(string $csvData, string $delimiter = ',', bool $skipHeader = false): array
24+
{
25+
$stream = fopen('php://memory', 'rb+');
26+
27+
if ($stream === false) {
28+
throw new \RuntimeException('Failed to open memory stream.');
29+
}
30+
31+
fwrite($stream, $csvData);
32+
rewind($stream);
33+
34+
$headerSkipped = false;
35+
$csvRows = [];
36+
while (($row = fgetcsv($stream, 0, $delimiter)) !== false) {
37+
if ($skipHeader && !$headerSkipped) {
38+
$headerSkipped = true;
39+
continue;
40+
}
41+
$csvRows[] = $row;
42+
}
43+
44+
// Ensure the stream is closed only if it's valid
45+
if (is_resource($stream)) {
46+
fclose($stream);
47+
}
48+
49+
return $csvRows;
50+
}
51+
}

‎Classes/Parser/WarnCellParser.php

+18-17
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,35 @@
1111

1212
namespace JWeiland\Weather2\Parser;
1313

14+
use JWeiland\Weather2\Helper\CsvHelper;
1415
use TYPO3\CMS\Core\Utility\GeneralUtility;
1516

1617
class WarnCellParser implements WarnCellParserInterface
1718
{
19+
public function __construct(private readonly CsvHelper $csvHelper)
20+
{
21+
}
22+
1823
/**
1924
* @return array<int, mixed>
2025
*/
21-
public function parse(string $data): array
26+
public function parse(string $warnCellData): array
2227
{
23-
$warnCellRecords = GeneralUtility::trimExplode(PHP_EOL, $data, true);
24-
array_shift($warnCellRecords); // Remove header row
25-
26-
$warnCellResults = [];
28+
$warnCellRecords = $this->csvHelper->convertCsvToArray($warnCellData, ';', true);
29+
$parsedWarnCells = [];
2730
foreach ($warnCellRecords as $index => $warnCellRecord) {
28-
$fields = str_getcsv($warnCellRecord, ';');
29-
if (count($fields) !== 5) {
30-
continue;
31+
// Ensure row has exactly 5 fields
32+
if (count($warnCellRecord) === 5) {
33+
[$warnCellId, $name, $shortName, , $sign] = $warnCellRecord; // Skip CCC field
34+
$parsedWarnCells[] = [
35+
'warn_cell_id' => $warnCellId,
36+
'name' => $name,
37+
'short_name' => $shortName,
38+
'sign' => $sign,
39+
];
3140
}
32-
33-
[$warnCellId, $name,$shortName, $sign] = $fields;
34-
$warnCellResults[] = [
35-
'warn_cell_id' => $warnCellId,
36-
'name' => $name,
37-
'short_name' => $shortName,
38-
'sign' => $sign,
39-
];
4041
}
4142

42-
return $warnCellResults;
43+
return $parsedWarnCells;
4344
}
4445
}

‎Classes/Parser/WarnCellParserInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ interface WarnCellParserInterface
1616
/**
1717
* @return array<int, mixed>
1818
*/
19-
public function parse(string $data): array;
19+
public function parse(string $warnCellData): array;
2020
}

‎Classes/Parser/WeatherAlertParser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class WeatherAlertParser implements WeatherAlertParserInterface
1616
public function parse(string $data): array
1717
{
1818
$pattern = '/^warnWetter\.loadWarnings\(|\);$/';
19-
$parseAlertData = json_decode(preg_replace($pattern, '', (string)$data), true);
19+
$parseAlertData = json_decode(preg_replace($pattern, '', $data), true);
2020
if ($parseAlertData === null) {
2121
throw new \UnexpectedValueException(
2222
'Response can not be parsed because it is an invalid string',

‎Classes/Service/DeutscherWetterdienstAlertService.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ protected function handleResponse(InputInterface $input, OutputInterface $output
4949
if (array_key_exists('warnings', $weatherAlertRecords)) {
5050
$this->processAlertData($weatherAlertRecords['warnings'], false, $input, $output);
5151
}
52+
5253
if (array_key_exists('vorabInformation', $weatherAlertRecords)) {
5354
$this->processAlertData($weatherAlertRecords['vorabInformation'], true, $input, $output);
5455
}
@@ -86,6 +87,7 @@ protected function processAlertData(array $data, bool $isPreliminaryInformation,
8687
foreach ($selectedWarnCells as $warnCellId) {
8788
$this->processWarnCellAlerts($warnCellId, $data, $isPreliminaryInformation, $recordStoragePid, $progressBar, $output);
8889
}
90+
8991
$progressBar->finish();
9092
$output->writeln('');
9193
}
@@ -133,6 +135,7 @@ protected function processAlert(
133135
} else {
134136
$this->insertNewAlert($alert, $warnCellId, $isPreliminaryInformation, $recordStoragePid, $output);
135137
}
138+
136139
$progressBar->advance();
137140
}
138141

@@ -173,27 +176,33 @@ protected function getWeatherAlertInstanceForAlert(
173176
if (isset($alert['level'])) {
174177
$weatherAlert['level'] = $alert['level'];
175178
}
179+
176180
if (isset($alert['type'])) {
177181
$weatherAlert['type'] = $alert['type'];
178182
}
183+
179184
if (isset($alert['headline'])) {
180185
$weatherAlert['title'] = $alert['headline'];
181186
}
187+
182188
if (isset($alert['description'])) {
183189
$weatherAlert['description'] = $alert['description'];
184190
}
191+
185192
if (isset($alert['instruction'])) {
186193
$weatherAlert['instruction'] = $alert['instruction'];
187194
}
195+
188196
if (isset($alert['start'])) {
189197
$startTime = new \DateTime();
190198
$startTime->setTimestamp((int)substr((string)$alert['start'], 0, -3));
191-
$weatherAlert['start_date'] = (int)$startTime->getTimestamp();
199+
$weatherAlert['start_date'] = $startTime->getTimestamp();
192200
}
201+
193202
if (isset($alert['end'])) {
194203
$endTime = new \DateTime();
195204
$endTime->setTimestamp((int)substr((string)$alert['end'], 0, -3));
196-
$weatherAlert['end_date'] = (int)$endTime->getTimestamp();
205+
$weatherAlert['end_date'] = $endTime->getTimestamp();
197206
}
198207

199208
return $weatherAlert;

‎Classes/Service/WeatherDataHandlerService.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -47,41 +47,53 @@ public function saveWeatherData(
4747
if (isset($responseClass->main->temp)) {
4848
$weatherObjectArray['temperature_c'] = (float)$responseClass->main->temp;
4949
}
50+
5051
if (isset($responseClass->main->pressure)) {
5152
$weatherObjectArray['pressure_hpa'] = (float)$responseClass->main->pressure;
5253
}
54+
5355
if (isset($responseClass->main->humidity)) {
5456
$weatherObjectArray['humidity_percentage'] = $responseClass->main->humidity;
5557
}
58+
5659
if (isset($responseClass->main->temp_min)) {
5760
$weatherObjectArray['min_temp_c'] = $responseClass->main->temp_min;
5861
}
62+
5963
if (isset($responseClass->main->temp_max)) {
6064
$weatherObjectArray['max_temp_c'] = $responseClass->main->temp_max;
6165
}
66+
6267
if (isset($responseClass->wind->speed)) {
6368
$weatherObjectArray['wind_speed_m_p_s'] = $responseClass->wind->speed;
6469
}
70+
6571
if (isset($responseClass->wind->deg)) {
6672
$weatherObjectArray['wind_speed_m_p_s'] = $responseClass->wind->deg;
6773
}
74+
6875
if (isset($responseClass->rain)) {
6976
$rain = (array)$responseClass->rain;
7077
$weatherObjectArray['rain_volume'] = (float)($rain['1h'] ?? 0.0);
7178
}
79+
7280
if (isset($responseClass->snow)) {
7381
$snow = (array)$responseClass->snow;
7482
$weatherObjectArray['snow_volume'] = (float)($snow['1h'] ?? 0.0);
7583
}
84+
7685
if (isset($responseClass->clouds->all)) {
7786
$weatherObjectArray['clouds_percentage'] = $responseClass->clouds->all;
7887
}
88+
7989
if (isset($responseClass->dt)) {
8090
$weatherObjectArray['measure_timestamp'] = $responseClass->dt;
8191
}
92+
8293
if (isset($responseClass->weather[0]->icon)) {
8394
$weatherObjectArray['icon'] = $responseClass->weather[0]->icon;
8495
}
96+
8597
if (isset($responseClass->weather[0]->id)) {
8698
$weatherObjectArray['condition_code'] = $responseClass->weather[0]->id;
8799
}
@@ -102,7 +114,7 @@ public function saveWeatherData(
102114

103115
public function clearCache(string $cacheIds): void
104116
{
105-
$cacheIdsArray = GeneralUtility::intExplode(',', $cacheIds, true);
117+
$cacheIdsArray = GeneralUtility::intExplode(',', $cacheIds);
106118
$this->cacheService->clearPageCache($cacheIdsArray);
107119
}
108120
}

‎Classes/UserFunc/Tca.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace JWeiland\Weather2\UserFunc;
1313

14+
use TYPO3\CMS\Backend\Utility\BackendUtility;
15+
1416
/**
1517
* TCA userFunc stuff
1618
*/
@@ -23,10 +25,24 @@ class Tca
2325
*/
2426
public function getDwdWarnCellTitle(array &$parameters): void
2527
{
28+
// Extract variables for better readability
29+
$tableName = $parameters['table'];
30+
$recordUid = $parameters['row']['uid'] ?? null;
31+
32+
// Handle missing uid or record gracefully
33+
if (!$recordUid) {
34+
$parameters['title'] = '(No UID provided)';
35+
return;
36+
}
37+
38+
// Fetch the record
39+
$record = BackendUtility::getRecord($tableName, $recordUid);
40+
41+
// Generate title with fallback values
2642
$parameters['title'] = sprintf(
2743
'%s (%s)',
28-
$parameters['row']['name'] ?? '',
29-
$parameters['row']['warn_cell_id'] ?? '',
44+
$parameters['row']['name'] ?? '(No Name)',
45+
$record['warn_cell_id'] ?? '(No Warn Cell ID)'
3046
);
3147
}
3248
}

‎Tests/Functional/Command/OpenWeatherMapCommandTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ class OpenWeatherMapCommandTest extends FunctionalTestCase
2727
protected array $testExtensionsToLoad = ['typo3conf/ext/weather2'];
2828

2929
private LoggerInterface $logger;
30+
3031
private RequestFactory $requestFactory;
32+
3133
private CacheService $cacheService;
34+
3235
private ConnectionPool $connectionPool;
36+
3337
private CommandTester $commandTester;
3438

3539
protected function setUp(): void

0 commit comments

Comments
 (0)
Please sign in to comment.