Completed
Push — master ( 239d1f...03f491 )
by Dev
14:47
created

PageUpdateMailNotifier::postUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace PiedWeb\CMSBundle\Service;
4
5
use DateInterval;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
8
use Symfony\Component\Mailer\MailerInterface;
9
use Symfony\Contracts\Translation\TranslatorInterface;
10
11
/**
12
 * Move it to a plugin (todo).
13
 */
14
class PageUpdateMailNotifier
15
{
16
    private $mailer;
17
    private $emailTo;
18
    private $emailFrom;
19
    private $appName;
20
    private $rootDir;
21
    private $interval;
22
    private $em;
23
    private $translator;
24
    private $page;
25
26
    public function __construct(
27
        string $page,
28
        MailerInterface $mailer,
29
        string $emailFrom,
30
        ?string $emailTo,
31
        string $appName,
32
        string $rootDir,
33
        string $interval, //minIntervalBetweenTwoNotification
34
        EntityManagerInterface $entityManager,
35
        TranslatorInterface $translator
36
    ) {
37
        $this->mailer = $mailer;
38
        $this->emailTo = $emailTo;
39
        $this->emailFrom = $emailFrom;
40
        $this->interval = $interval;
41
        $this->appName = $appName;
42
        $this->rootDir = $rootDir;
43
        $this->em = $entityManager;
44
        $this->translator = $translator;
45
        $this->page = $page;
46
    }
47
48
    protected function getPageUpdatedSince($lastTime)
49
    {
50
        $query = $this->em->createQuery(
51
            'SELECT p FROM '.$this->page.' p WHERE p.createdAt > :lastTime OR p.updatedAt > :lastTime'
52
        )->setParameter('lastTime', $lastTime);
53
54
        return $query->getResult();
55
    }
56
57
    public function postUpdate($page)
0 ignored issues
show
Unused Code introduced by
The parameter $page is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

57
    public function postUpdate(/** @scrutinizer ignore-unused */ $page)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59
        $this->send();
60
    }
61
62
    public function postPersist($page)
0 ignored issues
show
Unused Code introduced by
The parameter $page is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

62
    public function postPersist(/** @scrutinizer ignore-unused */ $page)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        $this->send();
65
    }
66
67
    public function send()
68
    {
69
        if (!$this->emailTo) {
70
            return;
71
        }
72
73
        $lastTime = new LastTime($this->rootDir.'/../var/lastPageUpdateNotification');
74
        if (false === $lastTime->wasRunSince(new DateInterval($this->interval))) {
75
            return;
76
        }
77
78
        $pages = $this->getPageUpdatedSince($lastTime->get('15 minutes ago'));
79
        if (empty($pages)) {
80
            return;
81
        }
82
83
        $message = (new TemplatedEmail())
84
            ->subject(
85
                $this->translator->trans('admin.page.update_notification.title', ['%appName%' => $this->appName])
86
            )
87
            ->from($this->emailFrom)
88
            ->to($this->emailTo)
89
            ->htmlTemplate('@PiedWebCMS/admin/pageUpdateMailNotification.html.twig')
90
            ->context([
91
                'appName' => $this->appName,
92
                'pages' => $pages,
93
            ]);
94
95
        $lastTime->set();
96
        $this->mailer->send($message);
97
    }
98
}
99