Skip to content

Commit

Permalink
IBX-7337: Added twig functions to get user preference
Browse files Browse the repository at this point in the history
  • Loading branch information
mikadamczyk committed Dec 19, 2023
1 parent 3827b5c commit a26d258
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/bundle/Core/Resources/config/templating.yml
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,11 @@ services:
$permissionResolver: '@Ibexa\Contracts\Core\Repository\PermissionResolver'
tags:
- { name: twig.extension }

Ibexa\Core\MVC\Symfony\Templating\Twig\Extension\UserPreferenceExtension:
autowire: true
autoconfigure: true

Ibexa\Core\MVC\Symfony\Templating\Twig\Extension\UserPreferenceRuntime:
autowire: true
autoconfigure: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Core\MVC\Symfony\Templating\Twig\Extension;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class UserPreferenceExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction(
'ibexa_get_user_preference_value',
[UserPreferenceRuntime::class, 'getUserPreferenceValue'],
),
new TwigFunction(
'ibexa_has_user_preference',
[UserPreferenceRuntime::class, 'hasUserPreference'],
),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Core\MVC\Symfony\Templating\Twig\Extension;

use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
use Ibexa\Contracts\Core\Repository\UserPreferenceService;
use Twig\Extension\RuntimeExtensionInterface;

final class UserPreferenceRuntime implements RuntimeExtensionInterface
{
private UserPreferenceService $userPreferenceService;

public function __construct(
UserPreferenceService $userPreferenceService
) {
$this->userPreferenceService = $userPreferenceService;
}

public function getUserPreferenceValue(string $identifier, string $default): string
{
try {
return $this->userPreferenceService->getUserPreference($identifier)->value;
} catch (NotFoundException|UnauthorizedException $e) {
return $default;
}
}

public function hasUserPreference(string $identifier): bool
{
try {
$this->userPreferenceService->getUserPreference($identifier);

return true;
} catch (NotFoundException|UnauthorizedException $e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Core\MVC\Symfony\Templating\Twig\Extension;

use Ibexa\Contracts\Core\Repository\UserPreferenceService;
use Ibexa\Contracts\Core\Repository\Values\UserPreference\UserPreference;
use Ibexa\Core\Base\Exceptions\NotFoundException;
use Ibexa\Core\MVC\Symfony\Templating\Twig\Extension\UserPreferenceExtension;
use Ibexa\Core\MVC\Symfony\Templating\Twig\Extension\UserPreferenceRuntime;
use Twig\Extension\RuntimeExtensionInterface;
use Twig\RuntimeLoader\RuntimeLoaderInterface;
use Twig\Test\IntegrationTestCase;

final class UserPreferenceExtensionTest extends IntegrationTestCase
{
protected function getRuntimeLoaders(): array
{
$userPreferenceService = $this->createUserPreferenceService();

return [
new class($userPreferenceService) implements RuntimeLoaderInterface {
private UserPreferenceService $userPreferenceService;

public function __construct(
UserPreferenceService $userPreferenceService
) {
$this->userPreferenceService = $userPreferenceService;
}

public function load(string $class): ?RuntimeExtensionInterface
{
if ($class === UserPreferenceRuntime::class) {
return new UserPreferenceRuntime($this->userPreferenceService);
}

return null;
}
},
];
}

protected function getFixturesDir(): string
{
return __DIR__ . '/_fixtures/user_preference_functions';
}

/**
* @return \Twig\Extension\ExtensionInterface[]
*/
protected function getExtensions(): array
{
return [
new UserPreferenceExtension(),
];
}

private function createUserPreferenceService(): UserPreferenceService
{
$callback = static function ($identifier): UserPreference {
if ($identifier === 'baz') {
throw new NotFoundException('User Preference', 14);
}

return new UserPreference([
'value' => 'bar',
]);
};

$userPreferenceService = $this->createMock(UserPreferenceService::class);
$userPreferenceService
->method('getUserPreference')
->willReturnCallback($callback);

return $userPreferenceService;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
"ibexa_get_user_preference_value" function
--TEMPLATE--
{{ ibexa_get_user_preference_value('foo', 'default') }}
{{ ibexa_get_user_preference_value('baz', 'default') }}
--DATA--
return [];
--EXPECT--
bar
default
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
"ibexa_has_user_preference" function
--TEMPLATE--
{{ ibexa_has_user_preference('foo') is same as(true) }}
{{ ibexa_has_user_preference('baz') is same as(false) }}
--DATA--
return [];
--EXPECT--
1
1

0 comments on commit a26d258

Please sign in to comment.