vendor/craue/formflow-bundle/Storage/DataManager.php line 117

Open in your IDE?
  1. <?php
  2. namespace Craue\FormFlowBundle\Storage;
  3. use Craue\FormFlowBundle\Form\FormFlowInterface;
  4. /**
  5.  * Manages data of flows and their steps.
  6.  *
  7.  * It uses the following data structure with {@link DataManagerInterface::STORAGE_ROOT} as name of the root element within the storage:
  8.  * <code>
  9.  *     DataManagerInterface::STORAGE_ROOT => [
  10.  *         name of the flow => [
  11.  *             instance id of the flow => [
  12.  *                 'data' => [] // the actual step data
  13.  *             ]
  14.  *         ]
  15.  *     ]
  16.  * </code>
  17.  *
  18.  * @author Christian Raue <christian.raue@gmail.com>
  19.  * @copyright 2011-2021 Christian Raue
  20.  * @license http://opensource.org/licenses/mit-license.php MIT License
  21.  */
  22. class DataManager implements ExtendedDataManagerInterface {
  23.     /**
  24.      * @var string Key for the actual step data.
  25.      */
  26.     const DATA_KEY 'data';
  27.     /**
  28.      * @var StorageInterface
  29.      */
  30.     private $storage;
  31.     /**
  32.      * @param StorageInterface $storage
  33.      */
  34.     public function __construct(StorageInterface $storage) {
  35.         $this->storage $storage;
  36.     }
  37.     /**
  38.      * {@inheritDoc}
  39.      */
  40.     public function getStorage() {
  41.         return $this->storage;
  42.     }
  43.     /**
  44.      * {@inheritDoc}
  45.      */
  46.     public function save(FormFlowInterface $flow, array $data) {
  47.         // handle file uploads
  48.         if ($flow->isHandleFileUploads()) {
  49.             array_walk_recursive($data, function(&$value$key) {
  50.                 if (SerializableFile::isSupported($value)) {
  51.                     $value = new SerializableFile($value);
  52.                 }
  53.             });
  54.         }
  55.         // drop old data
  56.         $this->drop($flow);
  57.         // save new data
  58.         $savedFlows $this->storage->get(DataManagerInterface::STORAGE_ROOT, []);
  59.         $savedFlows array_merge_recursive($savedFlows, [
  60.             $flow->getName() => [
  61.                 $flow->getInstanceId() => [
  62.                     self::DATA_KEY => $data,
  63.                 ],
  64.             ],
  65.         ]);
  66.         $this->storage->set(DataManagerInterface::STORAGE_ROOT$savedFlows);
  67.     }
  68.     /**
  69.      * {@inheritDoc}
  70.      */
  71.     public function load(FormFlowInterface $flow) {
  72.         $data = [];
  73.         // try to find data for the given flow
  74.         $savedFlows $this->storage->get(DataManagerInterface::STORAGE_ROOT, []);
  75.         if (isset($savedFlows[$flow->getName()][$flow->getInstanceId()][self::DATA_KEY])) {
  76.             $data $savedFlows[$flow->getName()][$flow->getInstanceId()][self::DATA_KEY];
  77.         }
  78.         // handle file uploads
  79.         if ($flow->isHandleFileUploads()) {
  80.             $tempDir $flow->getHandleFileUploadsTempDir();
  81.             array_walk_recursive($data, function(&$value$key) use ($tempDir) {
  82.                 if ($value instanceof SerializableFile) {
  83.                     $value $value->getAsFile($tempDir);
  84.                 }
  85.             });
  86.         }
  87.         return $data;
  88.     }
  89.     /**
  90.      * {@inheritDoc}
  91.      */
  92.     public function exists(FormFlowInterface $flow) {
  93.         $savedFlows $this->storage->get(DataManagerInterface::STORAGE_ROOT, []);
  94.         return isset($savedFlows[$flow->getName()][$flow->getInstanceId()][self::DATA_KEY]);
  95.     }
  96.     /**
  97.      * {@inheritDoc}
  98.      */
  99.     public function drop(FormFlowInterface $flow) {
  100.         $savedFlows $this->storage->get(DataManagerInterface::STORAGE_ROOT, []);
  101.         // remove data for only this flow instance
  102.         unset($savedFlows[$flow->getName()][$flow->getInstanceId()]);
  103.         $this->storage->set(DataManagerInterface::STORAGE_ROOT$savedFlows);
  104.     }
  105.     /**
  106.      * {@inheritDoc}
  107.      */
  108.     public function listFlows() {
  109.         return array_keys($this->storage->get(DataManagerInterface::STORAGE_ROOT, []));
  110.     }
  111.     /**
  112.      * {@inheritDoc}
  113.      */
  114.     public function listInstances($name) {
  115.         $savedFlows $this->storage->get(DataManagerInterface::STORAGE_ROOT, []);
  116.         if (array_key_exists($name$savedFlows)) {
  117.             return array_keys($savedFlows[$name]);
  118.         }
  119.         return [];
  120.     }
  121.     /**
  122.      * {@inheritDoc}
  123.      */
  124.     public function dropAll() {
  125.         $this->storage->remove(DataManagerInterface::STORAGE_ROOT);
  126.     }
  127. }