73 lines
2.3 KiB
PHP
73 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Temporal\DataEnrichment;
|
|
|
|
use App\Models\EnrichmentResult;
|
|
use App\Temporal\Shared\FaultSimulator;
|
|
use Temporal\Activity;
|
|
|
|
class DataEnrichmentActivity implements DataEnrichmentActivityInterface
|
|
{
|
|
public function geocodeAddress(array $record, array $simulationConfig = []): array
|
|
{
|
|
FaultSimulator::maybeApply($simulationConfig, 'geocodeAddress');
|
|
|
|
// Simulate geocoding API latency
|
|
usleep(mt_rand(200000, 500000));
|
|
|
|
return [
|
|
'lat' => round(mt_rand(2500, 4800) / 100, 6),
|
|
'lng' => round(mt_rand(-12200, -7100) / 100, 6),
|
|
'confidence' => round(mt_rand(70, 99) / 100, 2),
|
|
'attempt' => Activity::getInfo()->attempt,
|
|
];
|
|
}
|
|
|
|
public function validateEmail(array $record, array $simulationConfig = []): array
|
|
{
|
|
FaultSimulator::maybeApply($simulationConfig, 'validateEmail');
|
|
|
|
// Simulate email validation API latency
|
|
usleep(mt_rand(50000, 150000));
|
|
|
|
$email = $record['customer_email'] ?? 'unknown@example.com';
|
|
$isValid = filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
|
|
|
|
return [
|
|
'valid' => $isValid,
|
|
'disposable' => mt_rand(1, 100) <= 5,
|
|
'deliverable' => $isValid && mt_rand(1, 100) <= 95,
|
|
'attempt' => Activity::getInfo()->attempt,
|
|
];
|
|
}
|
|
|
|
public function calculateCreditScore(array $record, array $simulationConfig = []): array
|
|
{
|
|
FaultSimulator::maybeApply($simulationConfig, 'calculateCreditScore');
|
|
|
|
// Simulate credit scoring API latency
|
|
usleep(mt_rand(300000, 600000));
|
|
|
|
return [
|
|
'score' => mt_rand(300, 850),
|
|
'risk_level' => ['low', 'medium', 'high'][array_rand(['low', 'medium', 'high'])],
|
|
'attempt' => Activity::getInfo()->attempt,
|
|
];
|
|
}
|
|
|
|
public function saveEnrichedRecord(int $recordId, array $enrichments): bool
|
|
{
|
|
EnrichmentResult::updateOrCreate(
|
|
['record_type' => 'order', 'record_id' => $recordId],
|
|
[
|
|
'geocode_result' => $enrichments['geocode'] ?? null,
|
|
'email_valid' => $enrichments['email']['valid'] ?? null,
|
|
'credit_score' => $enrichments['credit']['score'] ?? null,
|
|
'enriched_at' => now(),
|
|
]
|
|
);
|
|
|
|
return true;
|
|
}
|
|
}
|