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 19, 2024
1 parent a8c0d45 commit ca0348e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
35 changes: 20 additions & 15 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 @@ -93,7 +93,7 @@ public function getConfigurationsByGroup(string $groupKey): array
public function getConfigurationValueByKey(string $key): string
{
$username = $this->getUsername();
$configurations = $this->repository->getConfigurationByUsernameAndKey($username, $key);
$configurations = $this->repository->getGlobalAndUserConfigurationByKey($username, $key);
$value = '';

/**
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);
}
}
3 changes: 2 additions & 1 deletion Services/Repository/ConfigRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getConfigurationByUsernameAndGroup(string $username, string $gro
->getResult();
}

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

Expand All @@ -60,6 +60,7 @@ public function getConfigurationByUsernameAndKey(string $username, string $key)
->setParameter('username', $username.'.'.$key)
->orWhere('c.id=:key')
->setParameter('key', $key)
->orderBy('c.isGlobal', 'DESC')
->getQuery()
->getResult();
}
Expand Down
2 changes: 1 addition & 1 deletion Services/Repository/ConfigRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface ConfigRepositoryInterface
{
public function getConfigurationByUsernameAndGroup(string $username, string $groupKey);

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

public function loadAllByGroup($groupKey, $valueOnly = false, $frontendOnly = false);

Expand Down

0 comments on commit ca0348e

Please sign in to comment.