![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mautic.corals.io/app/bundles/FormBundle/Collector/ |
<?php declare(strict_types=1); namespace Mautic\FormBundle\Collector; use Mautic\CacheBundle\Cache\CacheProviderInterface; /** * We need to store mapped fields in the form field builder so we could remove the used ones from the select box. */ final class AlreadyMappedFieldCollector implements AlreadyMappedFieldCollectorInterface { private const EXPIRATION_IN_SECONDS = 18000; public function __construct( private CacheProviderInterface $cacheProvider ) { } public function getFields(string $formId, string $object): array { $cacheItem = $this->cacheProvider->getItem($this->buildCacheKey($formId, $object)); return json_decode($cacheItem->get() ?? '[]', true); } public function addField(string $formId, string $object, string $fieldKey): void { $this->fetchAndSave($formId, $object, function (array $fields) use ($fieldKey): array { if (!in_array($fieldKey, $fields, true)) { $fields[] = $fieldKey; } return $fields; }); } public function removeField(string $formId, string $object, string $fieldKey): void { $this->fetchAndSave($formId, $object, function (array $fields) use ($fieldKey) { $cacheKey = array_search($fieldKey, $fields, true); if (false !== $cacheKey) { unset($fields[$cacheKey]); // Reset indexes. $fields = array_values($fields); } return $fields; }); } public function removeAllForForm(string $formId): void { $this->cacheProvider->invalidateTags([$this->buildCacheTag($formId)]); } private function fetchAndSave(string $formId, string $object, callable $callback): void { $cacheItem = $this->cacheProvider->getItem($this->buildCacheKey($formId, $object)); $fields = json_decode($cacheItem->get() ?? '[]', true); $cacheItem->set(json_encode($callback($fields))); $cacheItem->expiresAfter(self::EXPIRATION_IN_SECONDS); $cacheItem->tag($this->buildCacheTag($formId)); $this->cacheProvider->save($cacheItem); } private function buildCacheKey(string $formId, string $object): string { return sprintf('mautic.form.%s.object.%s.fields.mapped', $formId, $object); } private function buildCacheTag(string $formId): string { return sprintf('mautic.form.%s.fields.mapped', $formId); } }