forked from FriendsOfSymfony/FOSMessageBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageExtension.php
109 lines (95 loc) · 3.13 KB
/
MessageExtension.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
107
108
109
<?php
namespace FOS\MessageBundle\Twig\Extension;
use FOS\MessageBundle\Security\ParticipantProviderInterface;
use FOS\MessageBundle\Model\ReadableInterface;
use FOS\MessageBundle\Provider\ProviderInterface;
use FOS\MessageBundle\Model\ThreadInterface;
use FOS\MessageBundle\Security\AuthorizerInterface;
class MessageExtension extends \Twig_Extension
{
protected $participantProvider;
protected $provider;
protected $authorizer;
protected $nbUnreadMessagesCache;
public function __construct(ParticipantProviderInterface $participantProvider, ProviderInterface $provider, AuthorizerInterface $authorizer)
{
$this->participantProvider = $participantProvider;
$this->provider = $provider;
$this->authorizer = $authorizer;
}
/**
* Returns a list of global functions to add to the existing list.
*
* @return array An array of global functions
*/
public function getFunctions()
{
return array(
'fos_message_is_read' => new \Twig_Function_Method($this, 'isRead'),
'fos_message_nb_unread' => new \Twig_Function_Method($this, 'getNbUnread'),
'fos_message_can_delete_thread' => new \Twig_Function_Method($this, 'canDeleteThread'),
'fos_message_deleted_by_participant' => new \Twig_Function_Method($this, 'isThreadDeletedByParticipant')
);
}
/**
* Tells if this readable (thread or message) is read by the current user
*
* @return boolean
*/
public function isRead(ReadableInterface $readable)
{
return $readable->isReadByParticipant($this->getAuthenticatedParticipant());
}
/**
* Checks if the participant can mark a thread as deleted
*
* @param ThreadInterface $thread
*
* @return boolean true if participant can mark a thread as deleted, false otherwise
*/
public function canDeleteThread(ThreadInterface $thread)
{
return $this->authorizer->canDeleteThread($thread);
}
/**
* Checks if the participant has marked the thread as deleted
*
* @param ThreadInterface $thread
*
* @return boolean true if participant has marked the thread as deleted, false otherwise
*/
public function isThreadDeletedByParticipant(ThreadInterface $thread)
{
return $thread->isDeletedByParticipant($this->getAuthenticatedParticipant());
}
/**
* Gets the number of unread messages for the current user
*
* @return int
*/
public function getNbUnread()
{
if (null === $this->nbUnreadMessagesCache) {
$this->nbUnreadMessagesCache = $this->provider->getNbUnreadMessages();
}
return $this->nbUnreadMessagesCache;
}
/**
* Gets the current authenticated user
*
* @return ParticipantInterface
*/
protected function getAuthenticatedParticipant()
{
return $this->participantProvider->getAuthenticatedParticipant();
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'fos_message';
}
}