|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Twig; |
|
4
|
|
|
|
|
5
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\User; |
|
6
|
|
|
|
|
7
|
|
|
class PointUrlExtension extends \Twig_Extension |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
private $pointDomain; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $pointScheme; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $pointBaseUrl; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(string $pointDomain, string $pointScheme, string $pointBaseUrl) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->pointDomain = $pointDomain; |
|
27
|
|
|
$this->pointScheme = $pointScheme; |
|
28
|
|
|
$this->pointBaseUrl = $pointBaseUrl; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
View Code Duplication |
public function getFunctions() |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
return [ |
|
34
|
|
|
new \Twig_SimpleFunction('point_avatar', [$this, 'avatarFunction']), |
|
35
|
|
|
new \Twig_SimpleFunction('point_avatar_small', [$this, 'avatarSmallFunction']), |
|
36
|
|
|
new \Twig_SimpleFunction('point_avatar_medium', [$this, 'avatarMediumFunction']), |
|
37
|
|
|
new \Twig_SimpleFunction('point_avatar_large', [$this, 'avatarLargeFunction']), |
|
38
|
|
|
new \Twig_SimpleFunction('point_user_url', [$this, 'userUrl']), |
|
39
|
|
|
new \Twig_SimpleFunction('point_user_blog_url', [$this, 'userBlogUrl']), |
|
40
|
|
|
new \Twig_SimpleFunction('point_post_url', [$this, 'postUrl']), |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
View Code Duplication |
public function getFilters() |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
|
|
new \Twig_SimpleFilter('point_avatar', [$this, 'avatarFunction']), |
|
48
|
|
|
new \Twig_SimpleFilter('point_avatar_small', [$this, 'avatarSmallFunction']), |
|
49
|
|
|
new \Twig_SimpleFilter('point_avatar_medium', [$this, 'avatarMediumFunction']), |
|
50
|
|
|
new \Twig_SimpleFilter('point_avatar_large', [$this, 'avatarLargeFunction']), |
|
51
|
|
|
new \Twig_SimpleFilter('point_user_url', [$this, 'userUrl']), |
|
52
|
|
|
new \Twig_SimpleFilter('point_user_blog_url', [$this, 'userBlogUrl']), |
|
53
|
|
|
new \Twig_SimpleFilter('point_post_url', [$this, 'postUrl']), |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function avatarSmallFunction(string $login): string |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->avatarFunction($login, User::AVATAR_SIZE_SMALL); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function avatarMediumFunction(string $login): string |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->avatarFunction($login, User::AVATAR_SIZE_MEDIUM); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function avatarLargeFunction(string $login): string |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->avatarFunction($login, User::AVATAR_SIZE_LARGE); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function avatarFunction(string $login, $size): string |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->getAvatarUrlByLogin($login, $size); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function userUrl(string $login): string |
|
78
|
|
|
{ |
|
79
|
|
|
return sprintf('%s://%s.%s/', $this->pointScheme, $login, $this->pointDomain); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function userBlogUrl(string $login): string |
|
83
|
|
|
{ |
|
84
|
|
|
return sprintf('%s://%s.%s/blog/', $this->pointScheme, $login, $this->pointDomain); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function postUrl(string $postId): string |
|
88
|
|
|
{ |
|
89
|
|
|
return sprintf('%s://%s/%s', $this->pointScheme, $this->pointDomain, $postId); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function getAvatarUrlByLogin(string $login, string $size): string |
|
93
|
|
|
{ |
|
94
|
|
|
if (!in_array($size, [User::AVATAR_SIZE_SMALL, User::AVATAR_SIZE_MEDIUM, User::AVATAR_SIZE_LARGE], true)) { |
|
95
|
|
|
throw new \InvalidArgumentException('Avatar size must be one of restricted variants. See User::AVATAR_SIZE_* constants.'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return sprintf('%s://%s/avatar/%s/%s', $this->pointScheme, $this->pointDomain, urlencode($login), $size); |
|
99
|
|
|
} |
|
100
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.