Skip to content

Commit

Permalink
[TASK] Fixed php stan and CLI fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hojalatheef committed Dec 6, 2024
1 parent 4edff8e commit 5e7bcb9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 34 deletions.
18 changes: 13 additions & 5 deletions Classes/Command/DeutscherWetterdienstCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected function processDwdItems(array $category, bool $isPreliminaryInformati
$recordStoragePid = (int)$input->getArgument('recordStoragePage');
foreach ($selectedWarnCells as $warnCellId) {
$dwdWarnCells = $this->getDwdRecordsFindByName(
htmlspecialchars(strip_tags($warnCellId ?? '')),
htmlspecialchars(strip_tags($warnCellId)),
);
$progressBar = new ProgressBar($output, count($dwdWarnCells));
$progressBar->start();
Expand All @@ -175,7 +175,7 @@ protected function processDwdItems(array $category, bool $isPreliminaryInformati
$alert,
$dwdWarnCell['uid'],
$isPreliminaryInformation,
$recordStoragePid
$recordStoragePid,
);
$this->insertRecord($row);
$progressBar->advance();
Expand All @@ -188,6 +188,9 @@ protected function processDwdItems(array $category, bool $isPreliminaryInformati
}
}

/**
* @param array<string, mixed> $row
*/
private function insertRecord(array $row): void
{
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('tx_weather2_domain_model_weatheralert');
Expand Down Expand Up @@ -251,12 +254,13 @@ protected function getBackendUserAuthentication(): BackendUserAuthentication
* Returns filled WeatherAlert instance
*
* @param array<string, mixed> $alert
* @return array<string, mixed>
*/
protected function getWeatherAlertInstanceForAlert(
array $alert,
int $warnCellId,
bool $isPreliminaryInformation,
int $recordStoragePid
int $recordStoragePid,
): array {
$weatherAlert['pid'] = $recordStoragePid;
$weatherAlert['dwd_warn_cell'] = $warnCellId;
Expand Down Expand Up @@ -317,6 +321,10 @@ protected function removeOldAlertsFromDb(): void
$queryBuilder->executeStatement();
}

/**
* @return array<int, mixed>
* @throws Exception
*/
protected function getDwdRecordsFindByName(string $name): array
{
$table = 'tx_weather2_domain_model_dwdwarncell';
Expand All @@ -330,8 +338,8 @@ protected function getDwdRecordsFindByName(string $name): array
->where(
$queryBuilder->expr()->or(
$queryBuilder->expr()->eq('name', $queryBuilder->createNamedParameter(trim($name))),
$queryBuilder->expr()->eq('warn_cell_id', $queryBuilder->createNamedParameter($name))
)
$queryBuilder->expr()->eq('warn_cell_id', $queryBuilder->createNamedParameter($name)),
),
)
->orderBy('uid', 'ASC');

Expand Down
15 changes: 12 additions & 3 deletions Classes/Command/DeutscherWetterdienstWarnCellCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ final class DeutscherWetterdienstWarnCellCommand extends Command
public function __construct(
protected readonly LoggerInterface $logger,
protected readonly RequestFactory $requestFactory,
protected readonly ConnectionPool $connectionPool
protected readonly ConnectionPool $connectionPool,
) {
parent::__construct();
}

protected function configure(): void
{
$this->setHelp(
'Calls the Deutscher Wetterdienst api and saves warn cells into database. Required before using DeutscherWetterdienstTask!'
'Calls the Deutscher Wetterdienst api and saves warn cells into database. Required before using DeutscherWetterdienstTask!',
);
}

Expand All @@ -56,7 +56,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
} catch (\Throwable $e) {
$this->logger->error(
sprintf('Error while updating warn cells: %s', $e->getMessage()),
['exception' => $e]
['exception' => $e],
);
$output->writeln($e->getMessage());
$output->writeln('<error>An error occurred. Check the logs for details.</error>');
Expand All @@ -82,6 +82,9 @@ protected function fetchWarnCellData(): ResponseInterface
}
}

/**
* @return array<int, mixed>
*/
private function parseResponse(ResponseInterface $response): array
{
$rawRows = explode(PHP_EOL, trim((string)$response->getBody()));
Expand All @@ -108,6 +111,9 @@ private function parseResponse(ResponseInterface $response): array
return $rows;
}

/**
* @param array<int, mixed> $rows
*/
private function updateDatabase(array $rows, OutputInterface $output): void
{
$connection = $this->connectionPool->getConnectionForTable('tx_weather2_domain_model_dwdwarncell');
Expand Down Expand Up @@ -139,6 +145,9 @@ private function doesRecordExist(Connection $connection, string $warnCellId): bo
return (int)$count > 0;
}

/**
* @param array<string, mixed> $row
*/
private function insertRecord(Connection $connection, array $row): void
{
$connection->insert('tx_weather2_domain_model_dwdwarncell', [
Expand Down
22 changes: 1 addition & 21 deletions Classes/Command/OpenWeatherMapCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand All @@ -39,7 +38,7 @@ public function __construct(
private readonly LoggerInterface $logger,
private readonly RequestFactory $requestFactory,
private readonly CacheService $cacheService,
private readonly ConnectionPool $connectionPool
private readonly ConnectionPool $connectionPool,
) {
parent::__construct();
}
Expand Down Expand Up @@ -122,18 +121,10 @@ private function checkResponseCode(ResponseInterface $response): bool
{
if ($response->getStatusCode() === 401) {
$this->logger->error(WeatherUtility::translate('message.api_response_401', 'openweatherapi'));
$this->sendMail(
'Error while requesting weather data',
WeatherUtility::translate('message.api_response_401', 'openweatherapi'),
);
return false;
}
if ($response->getStatusCode() !== 200) {
$this->logger->error(WeatherUtility::translate('message.api_response_null', 'openweatherapi'));
$this->sendMail(
'Error while requesting weather data',
WeatherUtility::translate('message.api_response_null', 'openweatherapi'),
);
return false;
}

Expand All @@ -145,10 +136,6 @@ private function checkResponseCode(ResponseInterface $response): bool
return true;
case '404':
$this->logger->error(WeatherUtility::translate('messages.api_code_404', 'openweatherapi'));
$this->sendMail(
'Error while requesting weather data',
WeatherUtility::translate('messages.api_code_404', 'openweatherapi'),
);
return false;
default:
$this->logger->error(
Expand All @@ -157,13 +144,6 @@ private function checkResponseCode(ResponseInterface $response): bool
(string)$response->getBody(),
),
);
$this->sendMail(
'Error while requesting weather data',
sprintf(
WeatherUtility::translate('messages.api_code_none', 'openweatherapi'),
(string)$response->getBody()
),
);
return false;
}
}
Expand Down
5 changes: 0 additions & 5 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

use JWeiland\Weather2\Controller\CurrentWeatherController;
use JWeiland\Weather2\Controller\WeatherAlertController;
use JWeiland\Weather2\Task\DeutscherWetterdienstTask;
use JWeiland\Weather2\Task\DeutscherWetterdienstTaskAdditionalFieldProvider;
use JWeiland\Weather2\Task\DeutscherWetterdienstWarnCellTask;
use JWeiland\Weather2\Task\OpenWeatherMapTask;
use JWeiland\Weather2\Task\OpenWeatherMapTaskAdditionalFieldProvider;
use TYPO3\CMS\Extbase\Utility\ExtensionUtility;

call_user_func(static function () {
Expand Down

0 comments on commit 5e7bcb9

Please sign in to comment.