<?php
namespace AppBundle\EventListeners;
use Pimcore;
use Pimcore\Config;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\DataObject\Product;
use Pimcore\Model\DataObject\Supplier;
use Website\Helpers\MongoExportHelper;
class MongoUpdater
{
/** @var MongoExportHelper */
protected $exportHelper;
protected $mongoConfig;
public function __construct($mongoConfig)
{
$this->mongoConfig = $mongoConfig;
}
/**
* @param ElementEventInterface $e
* @throws \Exception
*/
public function handleEvent(ElementEventInterface $e)
{
if (!$e instanceof Pimcore\Event\Model\DataObjectEvent) {
return;
}
$object = $e->getObject();
if (env('DISABLE_MONGO_EXPORT', false)) {
return;
}
if ($object instanceof Product) {
return $this->handleProduct($object);
}
if ($object instanceof Supplier) {
return $this->handleSupplier($object);
}
}
/**
* @param Product $product
* @throws \Exception
*/
protected function handleProduct(Product $product)
{
$productId = $product->getId();
Product::setGetInheritedValues(true);
$this->connectToMongo();
$this->exportHelper()->updateProduct($productId);
}
/**
* @throws \Exception
*/
protected function connectToMongo()
{
$this->exportHelper()->connectToMongo(
$this->mongoConfig['hosts'],
$this->mongoConfig['username'],
$this->mongoConfig['password'],
$this->mongoConfig['database']);
}
/**
* @return MongoExportHelper
*/
protected function exportHelper()
{
if (!$this->exportHelper) {
$this->exportHelper = new MongoExportHelper();
}
return $this->exportHelper;
}
/**
* @param Supplier $supplier
* @throws \Exception
*/
protected function handleSupplier(Supplier $supplier)
{
$this->connectToMongo();
$this->exportHelper()->updateSupplier($supplier);
}
}