toBackfill[self::RESOURCES]; foreach ($api->getResources() as $site => $url) { $site = ResourceSite::from($site); if (! in_array($site, $toBackfill)) { Log::info("Resource {$site->localize()} should not be backfilled for {$this->label()} {$this->getModel()->getName()}"); continue; } if ($this->getModel()->resources()->getQuery()->where(ExternalResource::ATTRIBUTE_SITE, $site->value)->exists()) { Log::info("Resource {$site->localize()} already exists for {$this->label()} {$this->getModel()->getName()}"); continue; } $this->createResource(Uri::of($url), $site, $this->getModel()); $this->backfilled($site, self::RESOURCES); } } /** * Create the images given the response. */ protected function forImages(ExternalApiAction $api): void { $toBackfill = $this->toBackfill[self::IMAGES]; foreach ($api->getImages() as $facet => $url) { $facet = ImageFacet::from($facet); if (! in_array($facet, $toBackfill)) { Log::info("Skipping {$facet->localize()} for {$this->label()} {$this->getModel()->getName()}"); continue; } if ($this->getModel()->images()->getQuery()->where(Image::ATTRIBUTE_FACET, $facet->value)->exists()) { Log::info("Image {$facet->localize()} already exists for {$this->label()} {$this->getModel()->getName()}"); continue; } $this->createImageFromUrl($url, $facet, $this->getModel()); $this->backfilled($facet, self::IMAGES); } } /** * Remove element already backfilled. */ protected function backfilled(mixed $enum, string $scope): void { $index = array_search($enum, $this->toBackfill[$scope]); if ($index !== false) { unset($this->toBackfill[$scope][$index]); } } /** * Get the model for the action. * * @return BaseModel&HasResources&HasImages */ abstract protected function getModel(): BaseModel; /** * Get the human-friendly label for the underlying model. */ protected function label(): string { return Str::headline(class_basename($this->getModel())); } }