bundles/VehicleMarketBundle/Service/CampaignSchedule.php line 52

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace VehicleMarketBundle\Service;
  4. use Pimcore\Model\DataObject\CampaignMapping;
  5. /**
  6.  * Answers whether a campaign is running on a given day, so the frontend can decide what to offer
  7.  * rather than trusting mere presence on a vehicle.
  8.  *
  9.  * ## Why this exists
  10.  *
  11.  * automedia delivers a vehicle's campaigns as a complete list that includes ones which have not
  12.  * started yet - "announced campaigns are sent before they begin, so you can prepare them" - and
  13.  * says plainly: "Decide what to display from validFrom / validUntil, not from presence in the
  14.  * list." A vehicle's `campaignNames` is therefore the raw delivered record, not a statement about
  15.  * what is live today.
  16.  *
  17.  * ## Where the dates live
  18.  *
  19.  * On `CampaignMapping` (one object per campaign id), not on the vehicle: a validity window is a
  20.  * property of the campaign, and the same campaign runs on many vehicles. That also means a campaign
  21.  * starting or ending needs no vehicle write at all - the frontend simply evaluates the window on
  22.  * every request, and a campaign goes live and expires on its own. The alternative, filtering at
  23.  * ingest time, would leave the reference-counted `Kampagne` facet wrong until the next push for
  24.  * each affected vehicle.
  25.  *
  26.  * ## The two rules that are easy to get wrong
  27.  *
  28.  * 1. **`validUntil` is exclusive** - "the campaign is no longer running on that day". An inclusive
  29.  *    comparison advertises every campaign one day too long.
  30.  * 2. **Unknown or dateless campaigns count as running.** The AS-IS CSV path (see
  31.  *    VehicleImportProcessingListener) writes `campaignNames` without ever creating a mapping, and
  32.  *    mappings written before this field existed carry no dates. Treating those as "not running"
  33.  *    would empty the Kampagne filter for the whole legacy inventory.
  34.  *
  35.  * Dates are plain days without time or zone (`YYYY-MM-DD` in the payload), so every comparison is
  36.  * made on the date part alone.
  37.  */
  38. class CampaignSchedule
  39. {
  40.     /**
  41.      * @var array<string, array{from: ?string, until: ?string}>|null lazily built name -> window,
  42.      *                                                              keyed by campaign name
  43.      */
  44.     private ?array $windows = null;
  45.     public function isRunning(string $name, ?\DateTimeInterface $on = null): bool
  46.     {
  47.         $window = $this->windows()[$name] ?? null;
  48.         if ($window === null) {
  49.             // Unknown campaign - see class docblock, rule 2.
  50.             return true;
  51.         }
  52.         $day = ($on ?? new \DateTimeImmutable())->format('Y-m-d');
  53.         if ($window['from'] !== null && $day < $window['from']) {
  54.             return false;
  55.         }
  56.         // Exclusive upper bound - see class docblock, rule 1.
  57.         if ($window['until'] !== null && $day >= $window['until']) {
  58.             return false;
  59.         }
  60.         return true;
  61.     }
  62.     /**
  63.      * @param array<int|string, string> $names
  64.      *
  65.      * @return array<int, string> the running names, input order preserved, keys reindexed
  66.      */
  67.     public function filterRunning(array $names, ?\DateTimeInterface $on = null): array
  68.     {
  69.         $running = [];
  70.         foreach ($names as $name) {
  71.             if ($this->isRunning($name, $on)) {
  72.                 $running[] = $name;
  73.             }
  74.         }
  75.         return $running;
  76.     }
  77.     /**
  78.      * One query for every mapping, kept for the lifetime of the request: the filter form asks about
  79.      * every value of the Kampagne dimension at once, and a per-name lookup would turn one page
  80.      * render into dozens of queries.
  81.      *
  82.      * Later mappings win on a duplicate name. Names are free text and "can change while a campaign
  83.      * runs", so two ids can transiently carry the same name; the newer object is the better guess
  84.      * and, more importantly, the choice is deterministic.
  85.      *
  86.      * @return array<string, array{from: ?string, until: ?string}>
  87.      */
  88.     private function windows(): array
  89.     {
  90.         if ($this->windows !== null) {
  91.             return $this->windows;
  92.         }
  93.         $windows = [];
  94.         foreach (CampaignMapping::getList() as $mapping) {
  95.             $name = $mapping->getCampaignName();
  96.             if (!is_string($name) || trim($name) === '') {
  97.                 continue;
  98.             }
  99.             $windows[$name] = [
  100.                 'from' => $mapping->getValidFrom()?->format('Y-m-d'),
  101.                 'until' => $mapping->getValidUntil()?->format('Y-m-d'),
  102.             ];
  103.         }
  104.         return $this->windows = $windows;
  105.     }
  106. }