Files
pricebuddy/app/Filament/Concerns/InteractsWithSearchProgress.php
Jeremy 687295a9fc Address branch review: soft-404 accuracy, coverage and duplication
The branch passed neither `lando phpcs` nor `lando artisan test`:

- SearchService::sanitizeUtf8 compared mb_convert_encoding() against false,
  which PHPStan rejects as always-true (it never returns false on PHP 8).
- ScrapeUrlTest used Http::fake() without importing the facade, so the
  soft-404 test errored instead of asserting. The feature was untested.

Soft-404 detection was also too loose and, where it did fire, ineffective:

- A bare "404" in a title matched real model numbers ("Roland SP-404",
  "Peugeot 404"), nulling the price of live products and marking them
  discontinued. It now only counts alongside error wording or alone.
- The template-404 body marker was unanchored, so a theme stylesheet
  mentioning it could condemn a product page. Each marker is now anchored
  to the attribute it belongs to.
- The injected schema.org Discontinued value was re-read through the
  store's availability match config, which finds no rule for it and falls
  back to its default (usually InStock) - losing the signal on exactly the
  well-configured stores. ScrapeUrl::resolveStockStatus() now honours an
  explicit not_found flag first, and all availability call sites use it.

Also: cover the progress-polling fix (it had no tests), extract the
logic duplicated across both search components into a concern, sanitize
title/image as well as html, and fix an unrelated flaky UrlTest that
compared a created_at-ordered relation against an id-ordered assertion.
2026-08-09 09:34:28 +10:00

90 lines
2.5 KiB
PHP

<?php
namespace App\Filament\Concerns;
use App\Services\SearchService;
/**
* Shared polling state for the components that display a background search's progress
* log (the create-via-search widget and the product source search page).
*
* The search itself runs in a queued job and reports progress through SearchService's
* cache-backed log; these components only mirror that cache into component state while
* the job is running.
*/
trait InteractsWithSearchProgress
{
/**
* @var array<int, array<string, mixed>>
*/
public array $progressLog = [];
public bool $showLog = false;
/**
* Timestamp of when the search job was started, or false when nothing is running.
*/
public false|string $inProgress = false;
/**
* Timestamp of when the search job completed, or false when it has not.
*/
public false|string $isComplete = false;
/**
* Called on poll from the frontend.
*
* A finished search has nothing left to report, so stop hitting the cache. The view
* drops its wire:poll once $isComplete is set, but a poll already in flight can still
* land after that.
*/
public function refreshProgress(): void
{
if ($this->isComplete && ! $this->inProgress) {
return;
}
$this->syncProgressFromService();
}
/**
* Mirror the service's cached log and job flags into component state.
*/
protected function syncProgressFromService(): void
{
$searchQuery = $this->resolveProgressSearchQuery();
if (! $searchQuery) {
return;
}
$service = $this->makeProgressSearchService($searchQuery);
$log = $service->getLog();
// Keep locally staged entries (eg "Preparing to search") until the job has
// written its first entry, otherwise the panel flashes empty.
if (! empty($log)) {
$this->progressLog = $log;
}
$this->inProgress = $service->getInProgress() ?: false;
$this->isComplete = $service->getIsComplete() ?: false;
}
/**
* The query whose progress this component is tracking.
*/
protected function resolveProgressSearchQuery(): ?string
{
return $this->searchQuery;
}
/**
* Build the service used to read progress. Override to scope it, eg to a product source.
*/
protected function makeProgressSearchService(string $searchQuery): SearchService
{
return SearchService::new($searchQuery);
}
}