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

EZP-30224: Make ez_user_settings twig global load user settings dynamically #876

Merged
merged 1 commit into from
Mar 5, 2019
Merged
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
80 changes: 9 additions & 71 deletions src/bundle/Templating/Twig/UserPreferencesGlobalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,100 +8,38 @@

namespace EzSystems\EzPlatformAdminUiBundle\Templating\Twig;

use EzSystems\EzPlatformUser\UserSetting\UserSettingService;
use EzSystems\EzPlatformAdminUi\UI\Config\ConfigWrapper;
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\Proxy\LazyLoadingInterface;
use EzSystems\EzPlatformUser\UserSetting\UserSettingArrayAccessor;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;

/**
* @internal
*
* @todo provide extensibility to map selected settings
* @todo should be moved to ezplatform-user
*/
class UserPreferencesGlobalExtension extends AbstractExtension implements GlobalsInterface
{
/** @var \EzSystems\EzPlatformUser\UserSetting\UserSettingService */
protected $userSettingService;
/** @var \EzSystems\EzPlatformUser\UserSetting\UserSettingArrayAccessor */
protected $userSettingArrayAccessor;

/**
* @param \EzSystems\EzPlatformUser\UserSetting\UserSettingService $userSettingService
* @param \EzSystems\EzPlatformUser\UserSetting\UserSettingArrayAccessor $userSettingArrayAccessor
*/
public function __construct(
UserSettingService $userSettingService
UserSettingArrayAccessor $userSettingArrayAccessor
) {
$this->userSettingService = $userSettingService;
$this->userSettingArrayAccessor = $userSettingArrayAccessor;
}

/**
* @return array
*
* @throws \EzSystems\EzPlatformAdminUi\Exception\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function getGlobals(): array
{
return [
'ez_user_settings' => $this->createConfigWrapper(),
];
}

/**
* Create lazy loaded configuration.
*
* @return \EzSystems\EzPlatformAdminUi\UI\Config\ConfigWrapper
*/
private function createConfigWrapper(): ConfigWrapper
{
$factory = new LazyLoadingValueHolderFactory();
$initializer = function (&$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer) {
$initializer = null;
$wrappedObject = new ConfigWrapper($this->getUserSettings());

return true;
};

return $factory->createProxy(ConfigWrapper::class, $initializer);
}
// has to use \ArrayAccess object due to BC promise

/**
* @return array
*
* @throws \EzSystems\EzPlatformAdminUi\Exception\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
private function getUserSettings(): array
{
return [
'timezone' => $this->getTimezoneValue(),
'character_counter' => $this->getCharacterCounterValue(),
'ez_user_settings' => $this->userSettingArrayAccessor,
];
}

/**
* @return string
*
* @throws \EzSystems\EzPlatformAdminUi\Exception\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
private function getTimezoneValue(): string
{
return $this->userSettingService->getUserSetting('timezone')->value;
}

/**
* @return string
*
* @throws \EzSystems\EzPlatformAdminUi\Exception\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
private function getCharacterCounterValue(): string
{
return $this->userSettingService->getUserSetting('character_counter')->value;
}
}