<?php
declare(strict_types=1);
namespace VehicleMarketBundle\Service;
use Pimcore\Model\DataObject\CampaignMapping;
/**
* Answers whether a campaign is running on a given day, so the frontend can decide what to offer
* rather than trusting mere presence on a vehicle.
*
* ## Why this exists
*
* automedia delivers a vehicle's campaigns as a complete list that includes ones which have not
* started yet - "announced campaigns are sent before they begin, so you can prepare them" - and
* says plainly: "Decide what to display from validFrom / validUntil, not from presence in the
* list." A vehicle's `campaignNames` is therefore the raw delivered record, not a statement about
* what is live today.
*
* ## Where the dates live
*
* On `CampaignMapping` (one object per campaign id), not on the vehicle: a validity window is a
* property of the campaign, and the same campaign runs on many vehicles. That also means a campaign
* starting or ending needs no vehicle write at all - the frontend simply evaluates the window on
* every request, and a campaign goes live and expires on its own. The alternative, filtering at
* ingest time, would leave the reference-counted `Kampagne` facet wrong until the next push for
* each affected vehicle.
*
* ## The two rules that are easy to get wrong
*
* 1. **`validUntil` is exclusive** - "the campaign is no longer running on that day". An inclusive
* comparison advertises every campaign one day too long.
* 2. **Unknown or dateless campaigns count as running.** The AS-IS CSV path (see
* VehicleImportProcessingListener) writes `campaignNames` without ever creating a mapping, and
* mappings written before this field existed carry no dates. Treating those as "not running"
* would empty the Kampagne filter for the whole legacy inventory.
*
* Dates are plain days without time or zone (`YYYY-MM-DD` in the payload), so every comparison is
* made on the date part alone.
*/
class CampaignSchedule
{
/**
* @var array<string, array{from: ?string, until: ?string}>|null lazily built name -> window,
* keyed by campaign name
*/
private ?array $windows = null;
public function isRunning(string $name, ?\DateTimeInterface $on = null): bool
{
$window = $this->windows()[$name] ?? null;
if ($window === null) {
// Unknown campaign - see class docblock, rule 2.
return true;
}
$day = ($on ?? new \DateTimeImmutable())->format('Y-m-d');
if ($window['from'] !== null && $day < $window['from']) {
return false;
}
// Exclusive upper bound - see class docblock, rule 1.
if ($window['until'] !== null && $day >= $window['until']) {
return false;
}
return true;
}
/**
* @param array<int|string, string> $names
*
* @return array<int, string> the running names, input order preserved, keys reindexed
*/
public function filterRunning(array $names, ?\DateTimeInterface $on = null): array
{
$running = [];
foreach ($names as $name) {
if ($this->isRunning($name, $on)) {
$running[] = $name;
}
}
return $running;
}
/**
* One query for every mapping, kept for the lifetime of the request: the filter form asks about
* every value of the Kampagne dimension at once, and a per-name lookup would turn one page
* render into dozens of queries.
*
* Later mappings win on a duplicate name. Names are free text and "can change while a campaign
* runs", so two ids can transiently carry the same name; the newer object is the better guess
* and, more importantly, the choice is deterministic.
*
* @return array<string, array{from: ?string, until: ?string}>
*/
private function windows(): array
{
if ($this->windows !== null) {
return $this->windows;
}
$windows = [];
foreach (CampaignMapping::getList() as $mapping) {
$name = $mapping->getCampaignName();
if (!is_string($name) || trim($name) === '') {
continue;
}
$windows[$name] = [
'from' => $mapping->getValidFrom()?->format('Y-m-d'),
'until' => $mapping->getValidUntil()?->format('Y-m-d'),
];
}
return $this->windows = $windows;
}
}