Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
config.php
1<?php
3
4
11
12
13final class Config implements IConfig
14{
15 // public constants
16 const IG_CTA = 'ig_cta';
17 const MESSENGER_CHAT = 'messenger_chat';
18 const MESSENGER_MENU = 'messenger_menu';
19 const BUSINESS_NAME = 'business';
20 const PAGE_CARD = 'page_card';
21 const PAGE_CTA = 'page_cta';
22 const PAGE_POST = 'page_post';
23 const THREAD_INTENT = 'thread_intent';
24
25 // Non public constants
26 private const FIELDS_MAP = [
27 self::PAGE_CTA => ConfigField\PageCta::class,
28 self::BUSINESS_NAME => ConfigField\Name::class,
29 self::PAGE_CARD => ConfigField\PageCard::class,
30 self::PAGE_POST => ConfigField\PagePost::class,
31 self::IG_CTA => ConfigField\InstagramCta::class,
32 self::THREAD_INTENT => ConfigField\ThreadIntent::class,
33 self::MESSENGER_MENU => ConfigField\MessengerMenu::class,
34 self::MESSENGER_CHAT => ConfigField\MessengerChat::class
35 ];
36
37 private const FIELDS_DEPENDENCIES = [
38 self::MESSENGER_MENU => [self::MESSENGER_CHAT],
39 self::THREAD_INTENT => [self::MESSENGER_CHAT],
40 ];
41
42 public const FACEBOOK_BUSINESS_CONFIG_TLL = 86400;
43 private const FACEBOOK_BUSINESS_CONFIG_CACHE_ID = 'facebook|business|config';
44
46 private static $current;
47
49 private $value = [];
50
58 private static function getField(string $name) : ConfigField\IConfigField
59 {
60 if($field = self::FIELDS_MAP[$name])
61 {
62 return new $field;
63 }
64 throw new Exception\UnknownFieldException("$name");
65 }
66 private static function getFieldCodes() : array
67 {
68 static $codes;
69 return $codes = $codes ?? array_keys(self::FIELDS_MAP);
70 }
71
72 public static function create(): IConfig
73 {
74 return new self();
75 }
76
81 public function toArray(): array
82 {
83 $result = array_reduce(
84 static::getFieldCodes(),
85 (function (array $result, string $code) : array
86 {
87 $field = $this::getField($code);
88 if($field::available())
89 {
90 if ($value = $this->get($code))
91 {
92 $result[$code] = $value;
93 }
94 elseif ($field::required())
95 {
97 }
98 }
99 return $result;
100
101 })->bindTo($this,$this),
102 array()
103 );
104 $keys = array_keys($result);
105 foreach ($this::FIELDS_DEPENDENCIES as $code => $dependencies)
106 {
107 if($result[$code] && count($undefined = array_diff($dependencies,$keys)) > 0)
108 {
109 throw new Exception\UnresolvedDependencyException("$code:".implode(',',$undefined));
110 }
111 }
112 return $result;
113 }
114
115 public function save() : bool
116 {
117 try
118 {
119 if($adapter = ServiceAdapter::loadFacebookService())
120 {
121 return $adapter->getConfig()->set($this)->isSuccess();
122 }
123 return false;
124 }
125 catch (\Throwable $exception)
126 {
127 return false;
128 }
129 }
130
131 public function set(string $name, $value) : IConfig
132 {
133 $field = static::getField($name);
134 if($field::available())
135 {
136 if($field::checkValue($value = $field::prepareValue($value)))
137 {
138 $this->value[$name] = $value;
139 return $this;
140 }
141 throw new Exception\InvalidFieldValue("$name");
142 }
143 return $this;
144 }
145
146 public function get(string $name)
147 {
148 return $this->value[$name] ?? null;
149 }
150
154 public function jsonSerialize(): array
155 {
156 return array_reduce(
157 static::getFieldCodes(),
158 function(array $result, string $code) : array
159 {
160 $field = self::getField($code);
161 $result[$code] = ['value' => $this->value[$code] ?? $field::getDefaultValue()];
162 if($field instanceof Fields\IAvailableFieldList)
163 {
164 $result[$code]['set'] = $field::getAvailableValues();
165 }
166 return $result;
167
168 },
169 array()
170 );
171 }
172
178 public static function load(): IConfig
179 {
180 if(!self::$current)
181 {
182 $cache = Application::getInstance()->getManagedCache();
183 if ($cache->read(self::FACEBOOK_BUSINESS_CONFIG_TLL,self::FACEBOOK_BUSINESS_CONFIG_CACHE_ID))
184 {
185 self::$current = self::create();
186 self::$current->value = $cache->get(self::FACEBOOK_BUSINESS_CONFIG_CACHE_ID);
187 }
188 elseif (
189 ($adapter = ServiceAdapter::loadFacebookService())
190 && ($response = $adapter->getConfig()->get())
191 && $response->isSuccess()
192 && $data = $response->fetch()
193 )
194 {
195 self::$current = array_reduce(
196 static::getFieldCodes(),
197 function(IConfig $instance, string $code) use ($data)
198 {
199 if($value = $data[mb_strtoupper($code)])
200 {
201 $instance->set($code,$value);
202 }
203 return $instance;
204 },
205 self::create()
206 );
207 $cache->set(self::FACEBOOK_BUSINESS_CONFIG_CACHE_ID,self::$current->value);
208 }
209 else
210 {
211 throw new Exception\ConfigLoadException("can't load current fbe configuration");
212 }
213 }
214 return self::$current;
215 }
216
221 public static function default(): IConfig
222 {
223 $instance = self::create();
224 foreach (static::getFieldCodes()as $code)
225 {
226 $instance = $instance->set($code,self::getField($code)::getDefaultValue());
227 }
228 return $instance;
229
230 }
231
237 public static function loadFromArray(array $array) : IConfig
238 {
239 return array_reduce(
240 static::getFieldCodes(),
241 function(IConfig $instance, string $code) use ($array)
242 {
243 return (array_key_exists($code,$array)? $instance->set($code,$array[$code]) : $instance);
244 },
245 self::create()
246 );
247 }
248
249 public static function clearCache()
250 {
251 Application::getInstance()->getManagedCache()->clean(self::FACEBOOK_BUSINESS_CONFIG_CACHE_ID);
252 self::$current = null;
253 }
254}