forked from ezsystems/ezplatform-demo
/
PremiumContentExtension.php
106 lines (94 loc) · 2.74 KB
/
PremiumContentExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace AppBundle\Twig;
use AppBundle\PremiumContent\HtmlRenderer;
use AppBundle\User\UserGroups;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
/**
* Twig helper for premium content.
*/
class PremiumContentExtension extends Twig_Extension
{
/** @var \AppBundle\PremiumContent\HtmlRenderer */
private $htmlRenderer;
/** @var \AppBundle\User\UserGroups */
private $userGroups;
/** @var int[] */
private $allowedUserGroupsLocationIds;
/** @var bool */
private $hasAccess;
/**
* @param \AppBundle\PremiumContent\HtmlRenderer $htmlRenderer
* @param \AppBundle\User\UserGroups $userGroups
* @param array $allowedUserGroupsLocationIds
*/
public function __construct(
HtmlRenderer $htmlRenderer,
UserGroups $userGroups,
array $allowedUserGroupsLocationIds
) {
$this->htmlRenderer = $htmlRenderer;
$this->userGroups = $userGroups;
$this->allowedUserGroupsLocationIds = $allowedUserGroupsLocationIds;
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'premium_content_extension';
}
/**
* Returns a list of functions to add to the existing list.
*
* @return Twig_SimpleFunction[]
*/
public function getFunctions()
{
return [
new Twig_SimpleFunction('ez_has_access_to_premium_content', [$this, 'hasAccessToPremiumContent']),
];
}
/**
* Returns a list of filters to add to the existing list.
*
* @return Twig_SimpleFilter[]
*/
public function getFilters()
{
return [
new Twig_SimpleFilter('previewPremiumContent', [$this, 'previewPremiumContent'], ['is_safe' => ['html']]),
];
}
/**
* Allows to display certain number of paragraphs.
*
* @param string $document
* @param int $numberOfDisplayedElements
*
* @return string
*/
public function previewPremiumContent(string $document, int $numberOfDisplayedElements = 2): string
{
return $this->htmlRenderer->renderElements($document, $numberOfDisplayedElements);
}
/**
* Checks if user has access to premium content.
*
* @return bool
*/
public function hasAccessToPremiumContent(): bool
{
if (null !== $this->hasAccess) {
return $this->hasAccess;
}
return $this->hasAccess = $this->userGroups->isCurrentUserInOneOfTheGroups($this->allowedUserGroupsLocationIds);
}
}