Skip to content

Commit

Permalink
feat: Add method to get value by username & key
Browse files Browse the repository at this point in the history
  • Loading branch information
maamun7 committed May 20, 2024
1 parent a8c0d45 commit 106c564
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
33 changes: 19 additions & 14 deletions Services/Manager/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class ConfigManager
private array $groups = [];

public function __construct(
private ConfigRepositoryInterface $repository,
private FormFactoryInterface $formFactory,
private TokenStorageInterface $tokenStorage,
private ConfigRepositoryInterface $repository,
private FormFactoryInterface $formFactory,
private TokenStorageInterface $tokenStorage,
private AuthorizationCheckerInterface $checker,
$configurationGroups = []
) {
$configurationGroups = [])
{
foreach ($configurationGroups as $group) {
$this->groups[$group->getNameSpace()] = $group;
}
Expand All @@ -42,7 +42,7 @@ public function getConfigurationGroups(): array
$groups = [];

foreach ($this->groups as $key => $group) {
$groups[str_replace($username.'.', '', $key)] = $group;
$groups[str_replace($username . '.', '', $key)] = $group;
}

return $groups;
Expand Down Expand Up @@ -77,8 +77,8 @@ public function getConfigurationsByGroup(string $groupKey): array
* @var BaseConfig $configuration
*/
foreach ($configurations as $configuration) {
$key = str_replace($username.'.', '', $configuration->getId());
$key = str_replace($groupKey.'.', '', $key);
$key = str_replace($username . '.', '', $configuration->getId());
$key = str_replace($groupKey . '.', '', $key);

if (str_contains($configuration->getId(), $username)) {
$results[$key] = $configuration->getValue();
Expand All @@ -102,7 +102,7 @@ public function getConfigurationValueByKey(string $key): string
foreach ($configurations as $configuration) {
$value = $configuration->getValue();

if (str_contains($configuration->getId(), $username.$key)) {
if (str_contains($configuration->getId(), $username . $key)) {
break;
}
}
Expand Down Expand Up @@ -176,12 +176,12 @@ public function saveUserGroupData($key, FormInterface $form)
$formData = $form->getData();

foreach ($formData as $k => $val) {
$checkBoxKey = $k.'Preference';
$checkBoxKey = $k . 'Preference';

if (array_key_exists($checkBoxKey, $formData)) {
if ($formData[$checkBoxKey]) {
unset($formData[$k]);
$this->repository->removeByKey($key.'.'.$k);
$this->repository->removeByKey($key . '.' . $k);
}

unset($types[$checkBoxKey]);
Expand Down Expand Up @@ -221,10 +221,10 @@ public function getUserConfigurationValuesByGroupKey($groupKey): array

if (str_contains($configuration->getId(), $username)) {
$values[$key] = $configuration->getValue();
$values[$key.'Preference'] = false;
$values[$key . 'Preference'] = false;
} elseif (!array_key_exists($key, $values)) {
$values[$key] = $configuration->getValue();
$values[$key.'Preference'] = true;
$values[$key . 'Preference'] = true;
}
}

Expand All @@ -248,7 +248,7 @@ public function getValueByKey(int $isGlobal, string $key): string
$key = str_replace("{$username}.", '', $key);

if (!$isGlobal) {
$key = $username.'.'.$key;
$key = $username . '.' . $key;
}

$result = $this->repository->getConfigurationValue($key);
Expand Down Expand Up @@ -322,4 +322,9 @@ public function getFrontendConfigValuesByGroup($group): array

return $items;
}

public function getGlobalAndUserConfigurationByKey(string $key): ?array
{
return $this->repository->getGlobalAndUserConfigurationByKey($this->getUsername(), $key);
}
}
16 changes: 16 additions & 0 deletions Services/Repository/ConfigRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,20 @@ public function getConfigurationValue($key)

return $configuration->getValue();
}

public function getGlobalAndUserConfigurationByKey(string $username, string $key)
{
$qb = $this->createQueryBuilder('c');

return $qb
->setCacheable(true)
->setCacheRegion('config_key')
->where('c.id=:username')
->setParameter('username', $username.'.'.$key)
->orWhere('c.id=:key')
->setParameter('key', $key)
->orderBy('c.isGlobal', 'DESC')
->getQuery()
->getResult();
}
}
2 changes: 2 additions & 0 deletions Services/Repository/ConfigRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public function save($key, $value, $type = null, bool $locked = false, bool $for
public function removeByKey($key);

public function getConfigurationValue($key);

public function getGlobalAndUserConfigurationByKey(string $username, string $key);
}

0 comments on commit 106c564

Please sign in to comment.