39725afc7e20068e9d25fdea0af447d0c32c117f
[yaffs-website] / web / core / modules / layout_builder / src / LayoutTempstoreRepository.php
1 <?php
2
3 namespace Drupal\layout_builder;
4
5 use Drupal\Core\TempStore\SharedTempStoreFactory;
6
7 /**
8  * Provides a mechanism for loading layouts from tempstore.
9  *
10  * @internal
11  */
12 class LayoutTempstoreRepository implements LayoutTempstoreRepositoryInterface {
13
14   /**
15    * The shared tempstore factory.
16    *
17    * @var \Drupal\Core\TempStore\SharedTempStoreFactory
18    */
19   protected $tempStoreFactory;
20
21   /**
22    * LayoutTempstoreRepository constructor.
23    *
24    * @param \Drupal\Core\TempStore\SharedTempStoreFactory $temp_store_factory
25    *   The shared tempstore factory.
26    */
27   public function __construct(SharedTempStoreFactory $temp_store_factory) {
28     $this->tempStoreFactory = $temp_store_factory;
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function get(SectionStorageInterface $section_storage) {
35     $id = $section_storage->getStorageId();
36     $tempstore = $this->getTempstore($section_storage)->get($id);
37     if (!empty($tempstore['section_storage'])) {
38       $storage_type = $section_storage->getStorageType();
39       $section_storage = $tempstore['section_storage'];
40
41       if (!($section_storage instanceof SectionStorageInterface)) {
42         throw new \UnexpectedValueException(sprintf('The entry with storage type "%s" and ID "%s" is invalid', $storage_type, $id));
43       }
44     }
45     return $section_storage;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function set(SectionStorageInterface $section_storage) {
52     $id = $section_storage->getStorageId();
53     $this->getTempstore($section_storage)->set($id, ['section_storage' => $section_storage]);
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function delete(SectionStorageInterface $section_storage) {
60     $id = $section_storage->getStorageId();
61     $this->getTempstore($section_storage)->delete($id);
62   }
63
64   /**
65    * Gets the shared tempstore.
66    *
67    * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
68    *   The section storage.
69    *
70    * @return \Drupal\Core\TempStore\SharedTempStore
71    *   The tempstore.
72    */
73   protected function getTempstore(SectionStorageInterface $section_storage) {
74     $collection = 'layout_builder.section_storage.' . $section_storage->getStorageType();
75     return $this->tempStoreFactory->get($collection);
76   }
77
78 }