vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/Model/DefaultMockup.php line 181

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\Model;
  15. use Pimcore\Logger;
  16. use Pimcore\Model\DataObject;
  17. class DefaultMockup implements ProductInterfaceLinkGeneratorAwareInterface
  18. {
  19.     /** @var int */
  20.     protected $id;
  21.     /** @var array */
  22.     protected $params;
  23.     /** @var array */
  24.     protected $relations;
  25.     /**
  26.      * contains link generators by class type (just for caching)
  27.      *
  28.      * @var array
  29.      */
  30.     protected static array $linkGenerators = [];
  31.     /**
  32.      * @param int $id
  33.      * @param array $params
  34.      * @param array $relations
  35.      */
  36.     public function __construct($id$params$relations)
  37.     {
  38.         $this->id $id;
  39.         $this->params $params;
  40.         $this->relations = [];
  41.         if ($relations) {
  42.             foreach ($relations as $relation) {
  43.                 $this->relations[$relation['fieldname']][] = ['id' => $relation['dest'], 'type' => $relation['type']];
  44.             }
  45.         }
  46.     }
  47.     public function getLinkGenerator(): ?DataObject\ClassDefinition\LinkGeneratorInterface
  48.     {
  49.         if ($classId $this->params['o_classId'] ?? null) {
  50.             return static::$linkGenerators[$classId] ??= DataObject\ClassDefinition::getById($classId)->getLinkGenerator();
  51.         }
  52.         return null;
  53.     }
  54.     /**
  55.      * @return array
  56.      */
  57.     public function getParams()
  58.     {
  59.         return $this->params;
  60.     }
  61.     /**
  62.      * @param string $key
  63.      *
  64.      * @return mixed
  65.      */
  66.     public function getParam($key)
  67.     {
  68.         return $this->params[$key] ?? null;
  69.     }
  70.     /**
  71.      * @param array $params
  72.      *
  73.      * @return $this
  74.      */
  75.     public function setParams($params)
  76.     {
  77.         $this->params $params;
  78.         return $this;
  79.     }
  80.     /**
  81.      * @return array
  82.      */
  83.     public function getRelations()
  84.     {
  85.         return $this->relations;
  86.     }
  87.     /**
  88.      * @param array $relations
  89.      *
  90.      * @return $this
  91.      */
  92.     public function setRelations($relations)
  93.     {
  94.         $this->relations $relations;
  95.         return $this;
  96.     }
  97.     /**
  98.      * @return int
  99.      */
  100.     public function getId()
  101.     {
  102.         return $this->id;
  103.     }
  104.     /**
  105.      * @param string $attributeName
  106.      *
  107.      * @return mixed
  108.      */
  109.     public function getRelationAttribute($attributeName)
  110.     {
  111.         $relationObjectArray = [];
  112.         if ($this->relations[$attributeName]) {
  113.             foreach ($this->relations[$attributeName] as $relation) {
  114.                 $relationObject \Pimcore\Model\Element\Service::getElementById($relation['type'], $relation['id']);
  115.                 if ($relationObject) {
  116.                     $relationObjectArray[] = $relationObject;
  117.                 }
  118.             }
  119.         }
  120.         if (count($relationObjectArray) == 1) {
  121.             return $relationObjectArray[0];
  122.         } elseif (count($relationObjectArray) > 1) {
  123.             return $relationObjectArray;
  124.         } else {
  125.             return null;
  126.         }
  127.     }
  128.     /**
  129.      * @param string $method
  130.      * @param array $args
  131.      *
  132.      * @return mixed
  133.      */
  134.     public function __call($method$args)
  135.     {
  136.         $attributeName $method;
  137.         if (substr($method03) == 'get') {
  138.             $attributeName lcfirst(substr($method3));
  139.         }
  140.         if (is_array($this->params) && array_key_exists($attributeName$this->params)) {
  141.             return $this->params[$attributeName];
  142.         }
  143.         if (is_array($this->relations) && array_key_exists($attributeName$this->relations)) {
  144.             $relation $this->getRelationAttribute($attributeName);
  145.             if ($relation) {
  146.                 return $relation;
  147.             }
  148.         }
  149.         $msg "Method $method not in Mockup implemented, delegating to object with id {$this->id}.";
  150.         if (\Pimcore::inDebugMode()) {
  151.             Logger::warn($msg);
  152.         } else {
  153.             Logger::info($msg);
  154.         }
  155.         $object $this->getOriginalObject();
  156.         if ($object) {
  157.             if (method_exists($object$method)) {
  158.                 return call_user_func_array([$object$method], $args);
  159.             }
  160.             $method 'get' ucfirst($method);
  161.             if (method_exists($object$method)) {
  162.                 return call_user_func_array([$object$method], $args);
  163.             }
  164.         }
  165.         throw new \Exception("Object with {$this->id} not found.");
  166.     }
  167.     public function getOriginalObject()
  168.     {
  169.         Logger::notice("Getting original object {$this->id}.");
  170.         return \Pimcore\Model\DataObject::getById($this->id);
  171.     }
  172.     /**
  173.      * called by default CommitOrderProcessor to get the product name to store it in the order item
  174.      * should be overwritten in mapped sub classes of product classes
  175.      *
  176.      * @return string|null
  177.      */
  178.     public function getOSName(): ?string
  179.     {
  180.         return $this->__call('getOSName', []);
  181.     }
  182.     /**
  183.      * called by default CommitOrderProcessor to get the product number to store it in the order item
  184.      * should be overwritten in mapped sub classes of product classes
  185.      *
  186.      * @return string|null
  187.      */
  188.     public function getOSProductNumber(): ?string
  189.     {
  190.         return $this->__call('getOSProductNumber', []);
  191.     }
  192.     /**
  193.      * returns array of categories.
  194.      * has to be overwritten either in pimcore object or mapped sub class.
  195.      *
  196.      * @return array
  197.      */
  198.     public function getCategories()
  199.     {
  200.         return $this->__call('getCategories', []);
  201.     }
  202. }