Passed
Push — master ( 3db25e...88be9e )
by Alexey
03:31
created

Notifier::sendUserSubscribersUpdatedNotification()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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