1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service\Telegram; |
4
|
|
|
|
5
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\{Telegram\Account, User, UserRenameEvent}; |
6
|
|
|
use Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Notifies Telegram users about some events |
10
|
|
|
*/ |
11
|
|
|
class Notifier |
12
|
|
|
{ |
13
|
|
|
/** @var AccountRepository */ |
14
|
|
|
private $accountsRepo; |
15
|
|
|
|
16
|
|
|
/** @var MessageSender */ |
17
|
|
|
private $messenger; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
public function __construct(AccountRepository $accountRepository, MessageSender $messenger) |
21
|
|
|
{ |
22
|
|
|
$this->accountsRepo = $accountRepository; |
23
|
|
|
$this->messenger = $messenger; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param UserRenameEvent[] $userRenameEvents |
28
|
|
|
*/ |
29
|
|
|
public function sendUsersRenamedNotification(array $userRenameEvents): void |
30
|
|
|
{ |
31
|
|
|
$accounts = $this->accountsRepo->findBy(['renameNotification' => true]); |
32
|
|
|
|
33
|
|
|
$this->messenger->sendMassTemplatedMessage($accounts, '@SkobkinPointTools/Telegram/users_renamed_notification.md.twig', ['events' => $userRenameEvents]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Send notification about changes in user's subscribers list |
38
|
|
|
* |
39
|
|
|
* @param User $user |
40
|
|
|
* @param User[] $subscribed |
41
|
|
|
* @param User[] $unsubscribed |
42
|
|
|
*/ |
43
|
|
|
public function sendUserSubscribersUpdatedNotification(User $user, array $subscribed, array $unsubscribed): bool |
44
|
|
|
{ |
45
|
|
|
/** @var Account $account */ |
46
|
|
|
$account = $this->accountsRepo->findOneBy( |
47
|
|
|
[ |
48
|
|
|
'user' => $user, |
49
|
|
|
'subscriberNotification' => true, |
50
|
|
|
] |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
if (null === $account) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->messenger->sendTemplatedMessage( |
58
|
|
|
$account, |
59
|
|
|
'@SkobkinPointTools/Telegram/user_subscribers_updated_notification.md.twig', |
60
|
|
|
[ |
61
|
|
|
'user' => $user, |
62
|
|
|
'subscribed' => $subscribed, |
63
|
|
|
'unsubscribed' => $unsubscribed, |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |