Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
StateService.php
1<?php
2
4
8
9final class StateService
10{
11 private const STORAGE_PREFIX = 'StateService';
12 private static self $instance;
13 private SessionLocalStorage $storage;
14
15 public function __construct()
16 {
17 $this->storage = Application::getInstance()->getLocalSession(self::STORAGE_PREFIX);
18 }
19
20 private function saveState(string $state, array $payload): void
21 {
22 $this->storage->set($state, $payload);
23 }
24
25 #region public api
26
27 public static function getInstance(): self
28 {
29 self::$instance ??= new self();
30
31 return self::$instance;
32 }
33
34 public function createState(array $payload, bool $appendTimestamp = true): string
35 {
36 $value = Json::encode($payload);
37 if ($appendTimestamp)
38 {
39 $value .= time();
40 }
41
42 $state = hash('sha224', $value);
43 $this->saveState($state, $payload);
44
45 return $state;
46 }
47
48 public function getPayload(string $state): ?array
49 {
50 $payload = $this->storage->get($state);
51 if (is_array($payload))
52 {
53 return $payload;
54 }
55
56 return null;
57 }
58
59 #endregion public api
60}
createState(array $payload, bool $appendTimestamp=true)