-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-7337: Added twig functions to get user preference
- Loading branch information
1 parent
3827b5c
commit a26d258
Showing
6 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/lib/MVC/Symfony/Templating/Twig/Extension/UserPreferenceExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
), | ||
]; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/lib/MVC/Symfony/Templating/Twig/Extension/UserPreferenceRuntime.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
tests/lib/MVC/Symfony/Templating/Twig/Extension/UserPreferenceExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...wig/Extension/_fixtures/user_preference_functions/get_user_preference_value.function.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
10 changes: 10 additions & 0 deletions
10
...ting/Twig/Extension/_fixtures/user_preference_functions/has_user_preference.function.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |