Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add method to get user merged config values #9

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading