mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
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.
61 lines
2.0 KiB
PHP
Executable File
61 lines
2.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Rules;
|
|
|
|
use App\Enums\AiFeature;
|
|
use App\Services\AutoCreateStore;
|
|
use App\Services\Helpers\IntegrationHelper;
|
|
use App\Services\ScrapeUrl;
|
|
use Closure;
|
|
use Illuminate\Contracts\Validation\DataAwareRule;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
|
|
class StoreUrl implements DataAwareRule, ValidationRule
|
|
{
|
|
protected array $data = [];
|
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
if (empty($value) || ! filter_var($value, FILTER_VALIDATE_URL)) {
|
|
$fail('The url must be a valid URL starting with http:// or https://');
|
|
|
|
return;
|
|
}
|
|
|
|
// When AI self-healing is enabled, defer store/scrape validation to
|
|
// Url::createFromUrl, which builds or repairs the store config via AI and
|
|
// surfaces its own error if it ultimately cannot. Only URL format is enforced here.
|
|
if (IntegrationHelper::isFeatureEnabled(AiFeature::Healing)) {
|
|
return;
|
|
}
|
|
|
|
$store = ScrapeUrl::new($value)->getStore();
|
|
|
|
$shouldCreateStore = data_get($this->data, 'data.create_store', true);
|
|
|
|
if (empty($store) && ! $shouldCreateStore) {
|
|
$fail('The domain does not belong to any stores');
|
|
}
|
|
|
|
if ($store) {
|
|
$scrape = ScrapeUrl::new($value)->scrape();
|
|
|
|
$availabilityStrategy = data_get($scrape, 'store.scrape_strategy.availability');
|
|
$isUnavailable = ScrapeUrl::resolveStockStatus($scrape, $availabilityStrategy)->isUnavailable();
|
|
|
|
if (empty($scrape['title']) || (empty($scrape['price']) && ! $isUnavailable)) {
|
|
$fail('The url does not contain a valid title or price');
|
|
}
|
|
} elseif ($shouldCreateStore && ! AutoCreateStore::canAutoCreateFromUrl($value)) {
|
|
$fail('Unable to auto create store');
|
|
}
|
|
}
|
|
|
|
public function setData(array $data): static
|
|
{
|
|
$this->data = $data;
|
|
|
|
return $this;
|
|
}
|
|
}
|