src/ConnectBundle/EventListener/ObjectListener.php line 45

Open in your IDE?
  1. <?php
  2. /**
  3.  * CONNECT BUNDLE
  4.  * Copyright © 2019 onacy GmbH
  5.  *
  6.  * According to our dual licensing model, this program can be used either
  7.  * under the terms of the GNU Affero General Public License, version 3,
  8.  * or under a proprietary license.
  9.  *
  10.  * The texts of the GNU Affero General Public License, supplemented by an additional
  11.  * permission, and of our proprietary license can be found
  12.  * in the LICENSE file you have received along with this program.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17.  * GNU Affero General Public License for more details.
  18.  *
  19.  * "shopware" is a registered trademark of shopware AG.
  20.  * "onacy" is a registered trademark of onacy GmbH.
  21.  * The licensing of the program under the AGPLv3 does not imply a
  22.  * trademark license. Therefore any rights, titles and interests in the
  23.  * above trademarks remain entirely with the trademark owners.
  24.  *
  25.  * @copyright  Copyright (c) 2019, onacy GmbH (http://www.onacy.de)
  26.  * @author     onacy GmbH <dev@onacy.de>
  27.  */
  28. namespace ConnectBundle\EventListener;
  29. use ConnectBundle\Components\Shopware\ArticleApi;
  30. use ConnectBundle\Components\Shopware\CategoryApi;
  31. use ConnectBundle\Components\Shopware\FilterSetApi;
  32. use ConnectBundle\Helper\ShopwareApi;
  33. use Pimcore\Event\Model\DataObjectEvent;
  34. use Pimcore\Event\Model\ElementEventInterface;
  35. use Pimcore\Model\DataObject;
  36. use Pimcore\Model\Version;
  37. class ObjectListener
  38. {
  39.     private $handled = array();
  40.     public function onPostUpdate(ElementEventInterface $e)
  41.     {
  42.         if (!$e instanceof DataObjectEvent) {
  43.             return;
  44.         }
  45.         $object $e->getObject();
  46.         if ($object instanceof DataObject\Concrete) {
  47.             /** @var Version $latestVersion */
  48.             $latestVersion end($object->getVersions());
  49.             if ($latestVersion->getUserId() === 0) {
  50.                 return; // Skip if change is from system user
  51.             }
  52.         }
  53.         if (isset($this->handled) && $this->handled[$object->getId()]) {
  54.             return;
  55.         }
  56.         $this->handled[$object->getId()] = true;
  57.         if ($object instanceof DataObject\Category) {
  58.             $categoryApi = new CategoryApi($this->getShopwareApi());
  59.             $categoryApi->load($object);
  60.             if ($categoryApi->upsert()) {
  61.                 $this->Logger()->info("Category update successful", [
  62.                     "relatedObject" => $object,
  63.                 ]);
  64.             }
  65.             else {
  66.                 $this->Logger()->warning("Category update not successful", [
  67.                     "relatedObject" => $object,
  68.                 ]);
  69.             }
  70.         }
  71.         //delete variant from sw if unpublished in pim
  72.         if ($object instanceof DataObject\Article) {
  73.         $time_start microtime(true);
  74.             $articleApi = new ArticleApi($this->getShopwareApi());
  75.             $articleApi->load($object);
  76.             if ($articleApi->upsert()) {
  77.         $time_end microtime(true);
  78.         $execution_time = ($time_end $time_start)/60;
  79.                 $this->Logger()->info("Article update successful", [
  80.                     "relatedObject" => $object,
  81.                 ]);
  82.                 $this->Logger()->debug("Article Speed Time " .$execution_time " Minutes");
  83.             }
  84.             else {
  85.                 $this->Logger()->warning("Article update not successful", [
  86.                     "relatedObject" => $object,
  87.                 ]);
  88.             }
  89.         }
  90.         if ($object instanceof DataObject\FilterSet) {
  91.             $filterSetApi = new FilterSetApi($this->getShopwareApi());
  92.             $filterSetApi->load($object);
  93.             if ($filterSetApi->upsert()) {
  94.                 $this->Logger()->info("Filter Set update successful", [
  95.                     "relatedObject" => $object,
  96.                 ]);
  97.             }
  98.             else {
  99.                 $this->Logger()->warning("Filter Set update not successful", [
  100.                     "relatedObject" => $object,
  101.                 ]);
  102.             }
  103.         }
  104.         if ($object instanceof DataObject\FilterGroup) {
  105.             // todo: implement filter group updater
  106.         }
  107.         if ($object instanceof DataObject\FilterOption) {
  108.             // todo: implement filter option updater
  109.         }
  110.         if ($object instanceof DataObject\Supplier) {
  111.             // todo: implement suppier updater
  112.         }
  113.     }
  114.     private $shopwareApi null;
  115.     private function getShopwareApi()
  116.     {
  117.         if ($this->shopwareApi === null) {
  118.             $this->shopwareApi = new ShopwareApi();
  119.         }
  120.         return $this->shopwareApi;
  121.     }
  122.     private $logger null;
  123.     private function Logger()
  124.     {
  125.         if ($this->logger === null) {
  126.             $this->logger = \Pimcore\Log\ApplicationLogger::getInstance("BwSyncOnSave"true);
  127.         }
  128.         return $this->logger;
  129.     }
  130. }