diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml index 442ff83..20e8cce 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml @@ -65,3 +65,11 @@ services: class: Skobkin\Bundle\PointToolsBundle\EventListener\UserRenameSubscriber tags: - { name: doctrine.event_subscriber, connection: default } + + # Twig extensions + point_tools.twig.point_avatar_extension: + class: Skobkin\Bundle\PointToolsBundle\Twig\PointAvatarExtension + public: false + arguments: ['@skobkin_point_tools.api_user'] + tags: + - { name: twig.extension } \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php b/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php index 55ef3e3..9037978 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php @@ -294,18 +294,31 @@ class UserApi extends AbstractApi } /** - * Creates avatar with specified size URL for user + * Creates URL of avatar with specified size by User object * * @param User $user * @param int $size * @return string */ public function getAvatarUrl(User $user, $size) + { + return $this->getAvatarUrlByLogin($user->getLogin(), $size); + } + + /** + * Creates URL of avatar with specified size by login string + * + * @param $login + * @param $size + * + * @return string + */ + public function getAvatarUrlByLogin($login, $size) { if (!in_array($size, [self::AVATAR_SIZE_SMALL, self::AVATAR_SIZE_MEDIUM, self::AVATAR_SIZE_LARGE], true)) { throw new \InvalidArgumentException('Avatar size must be one of restricted variants. See UserApi class AVATAR_SIZE_* constants.'); } - return $this->avatarsBaseUrl.urlencode($user->getLogin()).'/'.$size; + return $this->avatarsBaseUrl.urlencode($login).'/'.$size; } } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Twig/PointAvatarExtension.php b/src/Skobkin/Bundle/PointToolsBundle/Twig/PointAvatarExtension.php new file mode 100644 index 0000000..cee18d4 --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Twig/PointAvatarExtension.php @@ -0,0 +1,53 @@ +userApi = $userApi; + } + + public function getFunctions() + { + return [ + new \Twig_SimpleFunction('point_avatar', [$this, 'pointAvatarFunction']), + new \Twig_SimpleFunction('point_avatar_small', [$this, 'pointAvatarSmallFunction']), + new \Twig_SimpleFunction('point_avatar_medium', [$this, 'pointAvatarMediumFunction']), + new \Twig_SimpleFunction('point_avatar_large', [$this, 'pointAvatarLargeFunction']), + ]; + } + + public function pointAvatarSmallFunction($login) + { + return $this->pointAvatarFunction($login, UserApi::AVATAR_SIZE_SMALL); + } + + public function pointAvatarMediumFunction($login) + { + return $this->pointAvatarFunction($login, UserApi::AVATAR_SIZE_MEDIUM); + } + + public function pointAvatarLargeFunction($login) + { + return $this->pointAvatarFunction($login, UserApi::AVATAR_SIZE_LARGE); + } + + public function pointAvatarFunction($login, $size) + { + return $this->userApi->getAvatarUrlByLogin($login, $size); + } + + public function getName() + { + return 'point_tools_avatars'; + } +} \ No newline at end of file