vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/Tools/PaymentProviderInstaller.php line 75

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\EcommerceFrameworkBundle\Tools;
  15. use Pimcore\Extension\Bundle\Installer\AbstractInstaller;
  16. use Pimcore\Model\DataObject\ClassDefinition;
  17. use Pimcore\Model\DataObject\ClassDefinition\Service;
  18. use Pimcore\Model\DataObject\Objectbrick;
  19. class PaymentProviderInstaller extends AbstractInstaller
  20. {
  21.     /**
  22.      * @var string // json source path
  23.      */
  24.     protected $bricksPath;
  25.     /**
  26.      * @var array //$brickKey => $brickImportJsonPath
  27.      */
  28.     protected $bricksToInstall = [];
  29.     /**
  30.      * {@inheritdoc}
  31.      */
  32.     public function canBeInstalled()
  33.     {
  34.         return !$this->isInstalled();
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     public function canBeUninstalled()
  40.     {
  41.         return $this->isInstalled();
  42.     }
  43.     public function install()
  44.     {
  45.         $this->installBricks();
  46.         return true;
  47.     }
  48.     public function uninstall()
  49.     {
  50.         $this->unInstallBricks();
  51.         return true;
  52.     }
  53.     /**
  54.      * {@inheritdoc}
  55.      */
  56.     public function isInstalled()
  57.     {
  58.         $installed false;
  59.         try {
  60.             // check if payment brick exists
  61.             foreach ($this->bricksToInstall as $brickKey => $brickFile) {
  62.                 $installed Objectbrick\Definition::getByKey($brickKey);
  63.             }
  64.         } catch (\Exception $e) {
  65.             // nothing to do
  66.         }
  67.         return (bool) $installed;
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      */
  72.     public function needsReloadAfterInstall()
  73.     {
  74.         return true;
  75.     }
  76.     protected function installBricks()
  77.     {
  78.         foreach ($this->bricksToInstall as $brickKey => $brickFile) {
  79.             self::installBrick($brickKey$this->bricksPath $brickFile);
  80.         }
  81.     }
  82.     protected function unInstallBricks()
  83.     {
  84.         foreach ($this->bricksToInstall as $brickKey => $brickFile) {
  85.             $brick Objectbrick\Definition::getByKey($brickKey);
  86.             if ($brick instanceof Objectbrick\Definition) {
  87.                 $brick->delete();
  88.             }
  89.         }
  90.     }
  91.     protected static function installBrick($brickKey$filepath)
  92.     {
  93.         try {
  94.             $brick Objectbrick\Definition::getByKey($brickKey);
  95.         } catch (\Exception $e) {
  96.             $brick null;
  97.         }
  98.         if (!$brick) {
  99.             $brick = new Objectbrick\Definition;
  100.             $brick->setKey($brickKey);
  101.             $json file_get_contents($filepath);
  102.             $success Service::importObjectBrickFromJson($brick$json);
  103.             if ($success) {
  104.                 $onlineOrderClass ClassDefinition::getByName('OnlineShopOrder');
  105.                 /** @var ClassDefinition\Data\Objectbricks $paymentProviderBrickField */
  106.                 $paymentProviderBrickField $onlineOrderClass->getFieldDefinition('paymentProvider');
  107.                 $allowedTypes $paymentProviderBrickField->getAllowedTypes();
  108.                 $paymentProviderBrickField->setAllowedTypes([$brickKey, ...$allowedTypes]);
  109.             }
  110.         }
  111.     }
  112. }