src/AppBundle/EventListener/AssetPreUpdateListener.php line 26

Open in your IDE?
  1. <?php
  2. namespace AppBundle\EventListener;
  3. use AppBundle\Exception\AssetDeployerException;
  4. use AppBundle\Services\SftpFileTransferService;
  5. use Exception;
  6. use Pimcore;
  7. use Pimcore\Event\Model\AssetEvent;
  8. use Pimcore\Model\Asset;
  9. use Pimcore\Model\WebsiteSetting;
  10. class AssetPreUpdateListener
  11. {
  12.     const TWIST_INTERNAL_DEPLOY_FOLDER_NAME "Dossier pimcore d'assets transferable vers twist (usage interne)";
  13.     const IS_DEPLOYED 'Fichier transféré';
  14.     const TARGET_FOLDER 'Dossier distant de dépot du fichier (pour twist)';
  15.     /**
  16.      * @param AssetEvent $event
  17.      * @throws AssetDeployerException
  18.      */
  19.     public function onSaveTwistInternalAsset(AssetEvent $event)
  20.     {
  21.         if (Pimcore::inAdmin()) {
  22.             $asset $event->getAsset();
  23.             if ($this->isAssetInTwistDeployFolder($asset)) {
  24.                 if (!$this->hasDeployProperties($asset)) {
  25.                     $this->addDeployProperties($asset);
  26.                 } else {
  27.                     if (!$this->isAlreadySent($asset)) {
  28.                         $this->sendToTwist($asset);
  29.                     }
  30.                 }
  31.             }
  32.         }
  33.     }
  34.     /**
  35.      * @param Asset $asset
  36.      * @return bool
  37.      */
  38.     protected function isAssetInTwistDeployFolder(Asset $asset): bool
  39.     {
  40.         $twistDeployFolder WebsiteSetting::getByName(self::TWIST_INTERNAL_DEPLOY_FOLDER_NAME)->getData();
  41.         if ($twistDeployFolder) {
  42.             $parent $asset->getParent();
  43.             while ($parent->getId() !== 1) {
  44.                 if ($parent->getId() === $twistDeployFolder->getId()) {
  45.                     return true;
  46.                 }
  47.                 $parent $parent->getParent();
  48.             }
  49.         }
  50.         return false;
  51.     }
  52.     /**
  53.      * @param Asset $asset
  54.      * @return bool
  55.      */
  56.     protected function hasDeployProperties(Asset $asset): bool
  57.     {
  58.         return $asset->hasProperty(self::IS_DEPLOYED) && $asset->hasProperty(self::TARGET_FOLDER);
  59.     }
  60.     /**
  61.      * @param Asset $asset
  62.      */
  63.     protected function addDeployProperties(Asset $asset)
  64.     {
  65.         $asset->setProperty(self::IS_DEPLOYED'bool'false);
  66.         $asset->setProperty(self::TARGET_FOLDER'text''');
  67.     }
  68.     /**
  69.      * @param Asset $asset
  70.      * @return bool
  71.      */
  72.     protected function isAlreadySent(Asset $asset): bool
  73.     {
  74.         return $asset->getProperty(self::IS_DEPLOYED);
  75.     }
  76.     /**
  77.      * @param Asset $asset
  78.      * @throws AssetDeployerException
  79.      * @throws Exception
  80.      */
  81.     protected function sendToTwist(Asset $asset)
  82.     {
  83.         $destination $asset->getProperty(self::TARGET_FOLDER);
  84.         if ($destination === '') {
  85.             throw new AssetDeployerException('you need to define target path for this feature');
  86.         }
  87.         SftpFileTransferService::transferFileToTwist($asset->getFileSystemPath(), $destination$asset->getFilename());
  88.         $asset->setProperty(self::IS_DEPLOYED'bool'true);
  89.     }
  90. }