src/AppBundle/EventListeners/MongoUpdater.php line 28

Open in your IDE?
  1. <?php
  2. namespace AppBundle\EventListeners;
  3. use Pimcore;
  4. use Pimcore\Config;
  5. use Pimcore\Event\Model\ElementEventInterface;
  6. use Pimcore\Model\DataObject\Product;
  7. use Pimcore\Model\DataObject\Supplier;
  8. use Website\Helpers\MongoExportHelper;
  9. class MongoUpdater
  10. {
  11.     /** @var MongoExportHelper */
  12.     protected $exportHelper;
  13.     protected $mongoConfig;
  14.     public function __construct($mongoConfig)
  15.     {
  16.         $this->mongoConfig $mongoConfig;
  17.     }
  18.     /**
  19.      * @param ElementEventInterface $e
  20.      * @throws \Exception
  21.      */
  22.     public function handleEvent(ElementEventInterface $e)
  23.     {
  24.         if (!$e instanceof Pimcore\Event\Model\DataObjectEvent) {
  25.             return;
  26.         }
  27.         $object $e->getObject();
  28.         if (env('DISABLE_MONGO_EXPORT'false)) {
  29.             return;
  30.         }
  31.         if ($object instanceof Product) {
  32.             return $this->handleProduct($object);
  33.         }
  34.         if ($object instanceof Supplier) {
  35.             return $this->handleSupplier($object);
  36.         }
  37.     }
  38.     /**
  39.      * @param Product $product
  40.      * @throws \Exception
  41.      */
  42.     protected function handleProduct(Product $product)
  43.     {
  44.         $productId $product->getId();
  45.         Product::setGetInheritedValues(true);
  46.         $this->connectToMongo();
  47.         $this->exportHelper()->updateProduct($productId);
  48.     }
  49.     /**
  50.      * @throws \Exception
  51.      */
  52.     protected function connectToMongo()
  53.     {
  54.         $this->exportHelper()->connectToMongo(
  55.             $this->mongoConfig['hosts'],
  56.             $this->mongoConfig['username'],
  57.             $this->mongoConfig['password'],
  58.             $this->mongoConfig['database']);
  59.     }
  60.     /**
  61.      * @return MongoExportHelper
  62.      */
  63.     protected function exportHelper()
  64.     {
  65.         if (!$this->exportHelper) {
  66.             $this->exportHelper = new MongoExportHelper();
  67.         }
  68.         return $this->exportHelper;
  69.     }
  70.     /**
  71.      * @param Supplier $supplier
  72.      * @throws \Exception
  73.      */
  74.     protected function handleSupplier(Supplier $supplier)
  75.     {
  76.         $this->connectToMongo();
  77.         $this->exportHelper()->updateSupplier($supplier);
  78.     }
  79. }