<?php
namespace App\Message;
use Carbon\Carbon;
use Pimcore\Model\DataObject\CatalogGridBanner;
use Pimcore\Model\DataObject\CatalogTopBanner;
class AVOMessage{
protected array $enabledModels = [
"Award" => AwardModel::class,
"Advantage" => AdvantageModel::class,
"Feature" => FeatureModel::class,
"Brand" => BrandModel::class,
"BrandCategoryImage" => BrandCategoryImageModel::class,
"Collection" => CollectionModel::class,
"CollectionCategoryImage" => CollectionCategoryImageModel::class,
"Category" => CategoryModel::class,
"Label" => ProductLabelModel::class,
"ProductCard" => ProductCardModel::class,
"ProductVariation" => ProductVariationModel::class,
"ProductProperty" => ProductPropertyModel::class,
"Store" => StoreModel::class,
"ProductStock" => ProductStockModel::class,
"ProductRelease" => ProductReleaseModel::class,
"EuProductRelease" => ProductReleaseModel::class,
"ProductsSet" => ProductsSetModel::class,
"CatalogTopBanner" => CatalogTopBannerModel::class,
"CatalogGridBanner" => CatalogGridBannerModel::class,
];
public string $action;
public string $model;
public array $data;
public int $timestamp;
protected DataModelInterface $dataModel;
protected Carbon $createdAt;
public function __construct(array $rawEventItem)
{
$this->action = $rawEventItem['action'];
$this->model = $rawEventItem['model'];
$this->data = $rawEventItem['data'];
$this->timestamp = $rawEventItem['timestamp'];
$this->createdAt = Carbon::createFromTimestamp($rawEventItem['timestamp']);
$this->dataModel = $this->getModelByName($rawEventItem['model']);
}
public function getActionName(): string
{
return $this->action;
}
public function getModel(): string
{
return $this->model;
}
public function createdAt(): Carbon
{
return $this->createdAt;
}
public function getDataModel(): DataModelInterface
{
return $this->dataModel;
}
public function defaultExecute(): ?bool
{
return $this->dataModel->{$this->action}();
}
/**
* @param string $modelName
* @return DataModelInterface
* @throws \Exception
*/
private function getModelByName(string $modelName): DataModelInterface
{
if(!isset($this->enabledModels[$modelName]))
{
throw new \Exception("Can not find model '{$modelName}' in Event Bus module");
}
return $this->enabledModels[$modelName]::create($this->data, $this);
}
}