Test Setup Failed
Push — master ( d6bd2d...bc363c )
by Alexey
03:03
created

Notifier::sendUserSubscribersUpdatedNotification()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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