<?php
/**
* CONNECT BUNDLE
* Copyright © 2019 onacy GmbH
*
* According to our dual licensing model, this program can be used either
* under the terms of the GNU Affero General Public License, version 3,
* or under a proprietary license.
*
* The texts of the GNU Affero General Public License, supplemented by an additional
* permission, and of our proprietary license can be found
* in the LICENSE file you have received along with this program.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* "shopware" is a registered trademark of shopware AG.
* "onacy" is a registered trademark of onacy GmbH.
* The licensing of the program under the AGPLv3 does not imply a
* trademark license. Therefore any rights, titles and interests in the
* above trademarks remain entirely with the trademark owners.
*
* @copyright Copyright (c) 2019, onacy GmbH (http://www.onacy.de)
* @author onacy GmbH <dev@onacy.de>
*/
namespace ConnectBundle\EventListener;
use ConnectBundle\Components\Shopware\ArticleApi;
use ConnectBundle\Components\Shopware\CategoryApi;
use ConnectBundle\Components\Shopware\FilterSetApi;
use ConnectBundle\Helper\ShopwareApi;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\DataObject;
use Pimcore\Model\Version;
class ObjectListener
{
private $handled = array();
public function onPostUpdate(ElementEventInterface $e)
{
if (!$e instanceof DataObjectEvent) {
return;
}
$object = $e->getObject();
if ($object instanceof DataObject\Concrete) {
/** @var Version $latestVersion */
$latestVersion = end($object->getVersions());
if ($latestVersion->getUserId() === 0) {
return; // Skip if change is from system user
}
}
if (isset($this->handled) && $this->handled[$object->getId()]) {
return;
}
$this->handled[$object->getId()] = true;
if ($object instanceof DataObject\Category) {
$categoryApi = new CategoryApi($this->getShopwareApi());
$categoryApi->load($object);
if ($categoryApi->upsert()) {
$this->Logger()->info("Category update successful", [
"relatedObject" => $object,
]);
}
else {
$this->Logger()->warning("Category update not successful", [
"relatedObject" => $object,
]);
}
}
//delete variant from sw if unpublished in pim
if ($object instanceof DataObject\Article) {
$time_start = microtime(true);
$articleApi = new ArticleApi($this->getShopwareApi());
$articleApi->load($object);
if ($articleApi->upsert()) {
$time_end = microtime(true);
$execution_time = ($time_end - $time_start)/60;
$this->Logger()->info("Article update successful", [
"relatedObject" => $object,
]);
$this->Logger()->debug("Article Speed Time " .$execution_time . " Minutes");
}
else {
$this->Logger()->warning("Article update not successful", [
"relatedObject" => $object,
]);
}
}
if ($object instanceof DataObject\FilterSet) {
$filterSetApi = new FilterSetApi($this->getShopwareApi());
$filterSetApi->load($object);
if ($filterSetApi->upsert()) {
$this->Logger()->info("Filter Set update successful", [
"relatedObject" => $object,
]);
}
else {
$this->Logger()->warning("Filter Set update not successful", [
"relatedObject" => $object,
]);
}
}
if ($object instanceof DataObject\FilterGroup) {
// todo: implement filter group updater
}
if ($object instanceof DataObject\FilterOption) {
// todo: implement filter option updater
}
if ($object instanceof DataObject\Supplier) {
// todo: implement suppier updater
}
}
private $shopwareApi = null;
private function getShopwareApi()
{
if ($this->shopwareApi === null) {
$this->shopwareApi = new ShopwareApi();
}
return $this->shopwareApi;
}
private $logger = null;
private function Logger()
{
if ($this->logger === null) {
$this->logger = \Pimcore\Log\ApplicationLogger::getInstance("BwSyncOnSave", true);
}
return $this->logger;
}
}