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
37 private const FIELDS_DEPENDENCIES = [
38 self::MESSENGER_MENU => [self::MESSENGER_CHAT],
39 self::THREAD_INTENT => [self::MESSENGER_CHAT],
43 private const FACEBOOK_BUSINESS_CONFIG_CACHE_ID =
'facebook|business|config';
46 private static $current;
58 private static function getField(
string $name) :
ConfigField\IConfigField
60 if($field = self::FIELDS_MAP[$name])
66 private static function getFieldCodes() : array
69 return $codes = $codes ?? array_keys(self::FIELDS_MAP);
83 $result = array_reduce(
84 static::getFieldCodes(),
85 (
function (array $result,
string $code) : array
87 $field = $this::getField($code);
88 if($field::available())
90 if ($value = $this->
get($code))
92 $result[$code] = $value;
94 elseif ($field::required())
101 })->bindTo($this,$this),
104 $keys = array_keys($result);
105 foreach ($this::FIELDS_DEPENDENCIES as $code => $dependencies)
107 if($result[$code] && count($undefined = array_diff($dependencies,$keys)) > 0)
119 if($adapter = ServiceAdapter::loadFacebookService())
121 return $adapter->getConfig()->set($this)->isSuccess();
125 catch (\Throwable $exception)
131 public function set(
string $name, $value) :
IConfig
133 $field = static::getField($name);
134 if($field::available())
136 if($field::checkValue($value = $field::prepareValue($value)))
138 $this->value[$name] = $value;
146 public function get(
string $name)
148 return $this->value[$name] ??
null;
157 static::getFieldCodes(),
158 function(array $result,
string $code) : array
160 $field = self::getField($code);
161 $result[$code] = [
'value' => $this->value[$code] ?? $field::getDefaultValue()];
162 if($field instanceof Fields\IAvailableFieldList)
164 $result[$code][
'set'] = $field::getAvailableValues();
183 if ($cache->read(self::FACEBOOK_BUSINESS_CONFIG_TLL,self::FACEBOOK_BUSINESS_CONFIG_CACHE_ID))
185 self::$current = self::create();
186 self::$current->value = $cache->get(self::FACEBOOK_BUSINESS_CONFIG_CACHE_ID);
189 ($adapter = ServiceAdapter::loadFacebookService())
190 && ($response = $adapter->getConfig()->get())
191 && $response->isSuccess()
192 && $data = $response->fetch()
195 self::$current = array_reduce(
196 static::getFieldCodes(),
197 function(
IConfig $instance,
string $code) use ($data)
199 if($value = $data[mb_strtoupper($code)])
201 $instance->
set($code,$value);
207 $cache->set(self::FACEBOOK_BUSINESS_CONFIG_CACHE_ID,self::$current->value);
214 return self::$current;
223 $instance = self::create();
224 foreach (static::getFieldCodes()as $code)
226 $instance = $instance->set($code,self::getField($code)::getDefaultValue());
240 static::getFieldCodes(),
241 function(
IConfig $instance,
string $code) use ($array)
243 return (array_key_exists($code,$array)? $instance->
set($code,$array[$code]) : $instance);
252 self::$current =
null;