Completed
Push — master ( 6130d0...57338d )
by Dev
24:32 queued 11:23
created

PageUpdateEmailNotifier::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 6
dl 0
loc 15
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace PiedWeb\CMSBundle\Service;
4
5
use DateTime;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
8
9
/**
10
 * Move it to a plugin (todo).
11
 */
12
class PageUpdateEmailNotifier
13
{
14
    private $mailer;
15
    private $emailSender;
16
    private $emailToNotify;
17
    private $router;
18
19
    public function __construct(
20
        string $appName,
21
        string $emailSender,
22
        string $emailToNotify,
23
        \Swift_Mailer $mailer,
24
        UrlGeneratorInterface $router,
25
        string $rootDir
26
    ) {
27
        $this->mailer = $mailer;
28
        $this->emailSender = $emailSender;
29
        $this->emailToNotify = $emailToNotify;
30
        $this->appName = $appName;
0 ignored issues
show
Bug Best Practice introduced by
The property appName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
        $this->router = $router;
32
        $this->rootDir = $rootDir;
0 ignored issues
show
Bug Best Practice introduced by
The property rootDir does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
        $this->filesystem = new Filesystem();
0 ignored issues
show
Bug Best Practice introduced by
The property filesystem does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
    }
35
36
    /**
37
     * Send max 1 notification per hour.
38
     */
39
    protected function isItTimeTonotify(): bool
40
    {
41
        $lastNotificationFilepath = $this->rootDir.'/../var/lastPageUpdateNotification';
42
43
        if (file_exists($lastNotificationFilepath)) {
44
            $lastNotificationSendAt = new DateTime(date('Y-m-d H:i:s.', filemtime($lastNotificationFilepath)));
45
            $now = new DateTime('now');
46
            if ($lastNotificationSendAt->diff($now)->format('%h') < 1) {
47
                return false;
48
            }
49
        } else {
50
            file_put_contents($lastNotificationFilepath, '');
51
        }
52
53
        $this->filesystem->touch($lastNotificationFilepath);
54
55
        return true;
56
    }
57
58
    public function postUpdate($page)
59
    {
60
        if (!$this->emailToNotify || false === $this->isItTimeTonotify()) {
61
            return;
62
        }
63
64
        $adminUrl = $this->router->generate(
65
            'admin_app_page_edit',
66
            ['id' => $page->getId()],
67
            UrlGeneratorInterface::ABSOLUTE_URL
68
        );
69
70
        $message = (new \Swift_Message('Update on '.$this->appName))
71
        ->setFrom($this->emailSender)
72
        ->setTo($this->emailToNotify)
73
        ->setBody(
74
            'La page `<a href="'.$adminUrl.'">'.$page->getSlug().'</a>` a été modifiée.',
75
            'text/html'
76
        );
77
78
        $this->mailer->send($message);
79
    }
80
}
81