Refactor error handling for invoice period flag and item row fetching, improve cURL timeout

- Simplify error handling in `invoice_period_flag_service` by directly returning empty arrays on exceptions, removing redundant cache warm-up logic.
- Increase `CURLOPT_TIMEOUT` to 30 in `economic_m.php` for more reliable network requests.
- Adjust unit tests to reflect updated cURL timeout value.
This commit is contained in:
Jeppe Bundgaard
2026-05-12 15:30:25 +02:00
parent c343b52b57
commit 5f36939833
3 changed files with 21 additions and 23 deletions
@@ -525,21 +525,12 @@ class invoice_period_flag_service
try {
$flags = (new redis())->get_invoice_period_automatic_flags($dateFrom, $dateTo);
} catch (Throwable) {
$flags = null;
return [];
}
if (!is_array($flags)) {
// Cache miss — warm on demand and re-fetch
$this->warmAutomaticFlagsForPeriod($dateFrom, $dateTo);
try {
$flags = (new redis())->get_invoice_period_automatic_flags($dateFrom, $dateTo);
} catch (Throwable) {
return [];
}
if (!is_array($flags)) {
return [];
}
}
if ($onlyCustomerNumbers === null) {
return $flags;
@@ -610,16 +601,11 @@ class invoice_period_flag_service
try {
$rows = (new redis())->get_invoice_period_order_item_rows($dateFrom, $dateTo);
} catch (Throwable) {
$rows = null;
return [];
}
if (!is_array($rows)) {
// Cache miss — fetch from DB and cache for next request
$rows = $this->fetchOrderItemRowsFromDb($dateFrom, $dateTo);
try {
(new redis())->cache_invoice_period_order_item_rows($dateFrom, $dateTo, $rows);
} catch (Throwable) {
}
return [];
}
$this->seedOrderItemsPreviewCacheFromRows($rows);
@@ -1733,9 +1719,21 @@ class invoice_period_flag_service
$currentVehicleType = $this->normalizePrimaryVehicleProductName($currentProductName);
$expectedVehicleType = $this->normalizePrimaryVehicleProductName($expectedProductName);
return $currentVehicleType !== ''
&& $expectedVehicleType !== ''
&& $currentVehicleType === $expectedVehicleType;
if ($currentVehicleType === '' || $expectedVehicleType === '') {
return false;
}
if ($currentVehicleType === $expectedVehicleType) {
return true;
}
// Allow a match if one normalized name's tokens are a subset of the other.
// E.g. "Indvendig vask Kassevogn" → "kassevogn" is a subset of
// "Kassevogn/varevogn" → "kassevogn varevogn", meaning the same vehicle type.
$currentTokens = explode(' ', $currentVehicleType);
$expectedTokens = explode(' ', $expectedVehicleType);
if (count($currentTokens) <= count($expectedTokens)) {
return array_diff($currentTokens, $expectedTokens) === [];
}
return array_diff($expectedTokens, $currentTokens) === [];
}
private function normalizePrimaryVehicleProductName(string $productName): string
@@ -93,7 +93,7 @@ class economic_m
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_CONNECTTIMEOUT => 3,
CURLOPT_TIMEOUT => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
@@ -186,7 +186,7 @@ it('bounds e-conomic curl calls below the PHP request timeout', function (): voi
foreach ([(string)$legacyContent, (string)$endpointContent] as $content) {
expect($content)->toContain('CURLOPT_CONNECTTIMEOUT => 3')
->and($content)->toContain('CURLOPT_TIMEOUT => 10')
->and($content)->toContain('CURLOPT_TIMEOUT => 30')
->and($content)->not->toContain('CURLOPT_TIMEOUT => 0');
}
});