vendor/blackbit/data-director/EventListener/AutomaticImportListener.php line 125

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright Blackbit digital Commerce GmbH <info@blackbit.de>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
  6.  *
  7.  * 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 General Public License for more details.
  8.  *
  9.  * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  10.  */
  11. namespace Blackbit\DataDirectorBundle\EventListener;
  12. use Blackbit\DataDirectorBundle\Controller\ImportController;
  13. use Blackbit\DataDirectorBundle\lib\Pim\Helper;
  14. use Blackbit\DataDirectorBundle\lib\Pim\Import\CallbackFunction;
  15. use Blackbit\DataDirectorBundle\lib\Pim\Item\ImporterInterface;
  16. use Blackbit\DataDirectorBundle\lib\Pim\Item\ItemMoldBuilder;
  17. use Blackbit\DataDirectorBundle\lib\Pim\RawData\Importer;
  18. use Blackbit\DataDirectorBundle\lib\Pim\Serializer;
  19. use Blackbit\DataDirectorBundle\model\Dataport;
  20. use Blackbit\DataDirectorBundle\model\DataportResource;
  21. use Blackbit\DataDirectorBundle\model\Fieldmapping;
  22. use Blackbit\DataDirectorBundle\model\ImportStatus;
  23. use Blackbit\DataDirectorBundle\model\PimcoreDbRepository;
  24. use Blackbit\DataDirectorBundle\model\Queue;
  25. use Blackbit\DataDirectorBundle\model\RawItem;
  26. use Blackbit\DataDirectorBundle\model\RawItemData;
  27. use Blackbit\DataDirectorBundle\Tools\Installer;
  28. use InvalidArgumentException;
  29. use Pimcore\Db;
  30. use Pimcore\Event\Model\AssetEvent;
  31. use Pimcore\Event\Model\ElementEventInterface;
  32. use Pimcore\Logger;
  33. use Pimcore\Model\AbstractModel;
  34. use Pimcore\Model\Asset;
  35. use Pimcore\Model\DataObject\AbstractObject;
  36. use Pimcore\Model\DataObject\Concrete;
  37. use Pimcore\Model\Element\DirtyIndicatorInterface;
  38. use Pimcore\Model\Element\ElementInterface;
  39. use Pimcore\Model\Element\Service;
  40. use Pimcore\Model\User;
  41. use Pimcore\Model\Version;
  42. use Pimcore\Tool;
  43. use Blackbit\DataDirectorBundle\lib\Pim\Cli;
  44. use Psr\Log\LoggerAwareInterface;
  45. use Psr\Log\LoggerAwareTrait;
  46. use Psr\Log\LoggerInterface;
  47. use Symfony\Component\HttpFoundation\Request;
  48. class AutomaticImportListener implements LoggerAwareInterface
  49. {
  50.     use LoggerAwareTrait;
  51.     private static $processCommandRegistered = [];
  52.     private static $dataports null;
  53.     private static $hasAutomaticDataports null;
  54.     /** @var ItemMoldBuilder */
  55.     private $itemMoldBuilder;
  56.     public function __construct(ItemMoldBuilder $itemMoldBuilder)
  57.     {
  58.         $this->itemMoldBuilder $itemMoldBuilder;
  59.     }
  60.     private static function getDataports() {
  61.         if(self::$dataports === null) {
  62.             self::$dataports Dataport::getInstance()->find([], 'name');
  63.         }
  64.         return self::$dataports;
  65.     }
  66.     private function hasAutomaticDataports() {
  67.         if(self::$hasAutomaticDataports === null) {
  68.             self::$hasAutomaticDataports false;
  69.             foreach (self::getDataports() as $dataport) {
  70.                 $sourceConfig $dataport['sourceconfig'];
  71.                 if (!empty($sourceConfig['autoImport'])) {
  72.                     self::$hasAutomaticDataports true;
  73.                     break;
  74.                 }
  75.             }
  76.         }
  77.         return self::$hasAutomaticDataports;
  78.     }
  79.     public function startImports(ElementEventInterface $e) {
  80.         try {
  81.             if ($e->getArgument('saveVersionOnly') && $e->getArgument('isAutoSave')) {
  82.                 return;
  83.             }
  84.         } catch (\InvalidArgumentException $exception) {
  85.         }
  86.         if(!$this->hasAutomaticDataports()) {
  87.             return;
  88.         }
  89.         $object $e->getElement();
  90.         $user Helper::getUser();
  91.         $queue Queue::getInstance();
  92.         $savedDraftVersion false;
  93.         try {
  94.             if ($e->getArgument('saveVersionOnly')) {
  95.                 $savedDraftVersion true;
  96.             }
  97.         } catch (\InvalidArgumentException $exception) {
  98.         }
  99.         $queue->create([
  100.             'command' => 'data-director:start-automatic-dataports '.$object->getId().' '.Service::getElementType($object).' --user='.$user->getId().($savedDraftVersion ' --draft-version':''),
  101.             'triggered_by' => $object->getRealFullPath().' got saved'.(($user instanceof User) ? ' by '.$user->getUsername() : ''),
  102.             'worker_id' => 'check_automatic_run'
  103.         ]);
  104.     }
  105.     public function deleteRawdata(ElementEventInterface $e)
  106.     {
  107.         if (\method_exists($e'getArgument')) {
  108.             try {
  109.                 $saveVersionOnly $e->getArgument('saveVersionOnly');
  110.                 if ($saveVersionOnly) {
  111.                     return;
  112.                 }
  113.             } catch (\InvalidArgumentException $exception) {
  114.             }
  115.         }
  116.         $object $e->getElement();
  117.         $objectType Service::getElementType($object);
  118.         $commandPrefix '"'.Cli::getPhpCli().'" '.realpath(PIMCORE_PROJECT_ROOT.DIRECTORY_SEPARATOR.'bin'.DIRECTORY_SEPARATOR.'console');
  119.         foreach (self::getDataports() as $dataport) {
  120.             $sourceConfig $dataport['sourceconfig'];
  121.             $targetConfig $dataport['targetconfig'];
  122.             if (!empty($sourceConfig['autoImport']) && empty($targetConfig['itemClass'])) {
  123.                 $itemMold $this->itemMoldBuilder->getItemMoldByClassId($sourceConfig['sourceClass'] ?? null);
  124.                 if ($object instanceof $itemMold) {
  125.                     Cli::exec($commandPrefix.' data-director:delete-rawdata --dataport='.$dataport['id'].' --object-id='.$object->getId().' --object-type='.$objectType);
  126.                 }
  127.             }
  128.         }
  129.     }
  130. }