NewMessageMailNotifier::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 9
dl 0
loc 20
rs 9.9666

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace PiedWeb\ConversationBundle\Service;
4
5
use DateInterval;
6
use Doctrine\ORM\EntityManagerInterface;
7
use PiedWeb\CMSBundle\Service\LastTime;
8
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
9
use Symfony\Component\Mailer\MailerInterface;
10
use Symfony\Contracts\Translation\TranslatorInterface;
11
12
/**
13
 * Move it to a plugin (todo).
14
 */
15
class NewMessageMailNotifier
16
{
17
    private $mailer;
18
    private $emailTo;
19
    private $emailFrom;
20
    private $appName;
21
    private $rootDir;
22
    private $interval;
23
    private $em;
24
    private $translator;
25
    private $message;
26
27
    /**
28
     * Undocumented function.
29
     *
30
     * @param string $message Entity
31
     */
32
    public function __construct(
33
        string $message,
34
        MailerInterface $mailer,
35
        string $emailFrom,
36
        ?string $emailTo,
37
        string $appName,
38
        string $rootDir,
39
        string $interval, //minIntervalBetweenTwoNotification
40
        EntityManagerInterface $entityManager,
41
        TranslatorInterface $translator
42
    ) {
43
        $this->mailer = $mailer;
44
        $this->emailTo = $emailTo;
45
        $this->emailFrom = $emailFrom;
46
        $this->interval = $interval;
47
        $this->appName = $appName;
48
        $this->rootDir = $rootDir;
49
        $this->em = $entityManager;
50
        $this->translator = $translator;
51
        $this->message = $message;
52
    }
53
54
    protected function getMessagesPostedSince($datetime)
55
    {
56
        $query = $this->em->createQuery(
57
            'SELECT m FROM '.$this->message.' m WHERE m.createdAt > :lastNotificationTime'
58
        )->setParameter('lastNotificationTime', $datetime);
59
60
        return $query->getResult();
61
    }
62
63
    public function send()
64
    {
65
        if (!$this->emailTo) {
66
            return;
67
        }
68
69
        $lastTime = new LastTime($this->rootDir.'/../var/lastNewMessageNotification');
70
        if (false === $lastTime->wasRunSince(new DateInterval($this->interval))) {
71
            return;
72
        }
73
74
        $messages = $this->getMessagesPostedSince($lastTime->get('15 minutes ago'));
75
        if (empty($messages)) {
76
            return;
77
        }
78
79
        $message = (new TemplatedEmail())
80
            ->subject(
81
                $this->translator->trans(
82
                    'admin.conversation.notification.title.'.(count($messages) > 1 ? 'plural' : 'singular'),
83
                    ['%appName%' => $this->appName]
84
                )
85
            )
86
            ->from($this->emailFrom)
87
            ->to($this->emailTo)
88
            ->htmlTemplate('@PiedWebConversation/notification.html.twig')
89
            ->context([
90
                'appName' => $this->appName,
91
                'messages' => $messages,
92
            ]);
93
94
        $lastTime->set();
95
        $this->mailer->send($message);
96
97
        return true;
98
    }
99
}
100