src/Message/AVOMessage.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Message;
  3. use Carbon\Carbon;
  4. use Pimcore\Model\DataObject\CatalogGridBanner;
  5. use Pimcore\Model\DataObject\CatalogTopBanner;
  6. class AVOMessage{
  7. protected array $enabledModels = [
  8. "Award" => AwardModel::class,
  9. "Advantage" => AdvantageModel::class,
  10. "Feature" => FeatureModel::class,
  11. "Brand" => BrandModel::class,
  12. "BrandCategoryImage" => BrandCategoryImageModel::class,
  13. "Collection" => CollectionModel::class,
  14. "CollectionCategoryImage" => CollectionCategoryImageModel::class,
  15. "Category" => CategoryModel::class,
  16. "Label" => ProductLabelModel::class,
  17. "ProductCard" => ProductCardModel::class,
  18. "ProductVariation" => ProductVariationModel::class,
  19. "ProductProperty" => ProductPropertyModel::class,
  20. "Store" => StoreModel::class,
  21. "ProductStock" => ProductStockModel::class,
  22. "ProductRelease" => ProductReleaseModel::class,
  23. "EuProductRelease" => ProductReleaseModel::class,
  24. "ProductsSet" => ProductsSetModel::class,
  25. "CatalogTopBanner" => CatalogTopBannerModel::class,
  26. "CatalogGridBanner" => CatalogGridBannerModel::class,
  27. ];
  28. public string $action;
  29. public string $model;
  30. public array $data;
  31. public int $timestamp;
  32. protected DataModelInterface $dataModel;
  33. protected Carbon $createdAt;
  34. public function __construct(array $rawEventItem)
  35. {
  36. $this->action = $rawEventItem['action'];
  37. $this->model = $rawEventItem['model'];
  38. $this->data = $rawEventItem['data'];
  39. $this->timestamp = $rawEventItem['timestamp'];
  40. $this->createdAt = Carbon::createFromTimestamp($rawEventItem['timestamp']);
  41. $this->dataModel = $this->getModelByName($rawEventItem['model']);
  42. }
  43. public function getActionName(): string
  44. {
  45. return $this->action;
  46. }
  47. public function getModel(): string
  48. {
  49. return $this->model;
  50. }
  51. public function createdAt(): Carbon
  52. {
  53. return $this->createdAt;
  54. }
  55. public function getDataModel(): DataModelInterface
  56. {
  57. return $this->dataModel;
  58. }
  59. public function defaultExecute(): ?bool
  60. {
  61. return $this->dataModel->{$this->action}();
  62. }
  63. /**
  64. * @param string $modelName
  65. * @return DataModelInterface
  66. * @throws \Exception
  67. */
  68. private function getModelByName(string $modelName): DataModelInterface
  69. {
  70. if(!isset($this->enabledModels[$modelName]))
  71. {
  72. throw new \Exception("Can not find model '{$modelName}' in Event Bus module");
  73. }
  74. return $this->enabledModels[$modelName]::create($this->data, $this);
  75. }
  76. }