• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / logging / #186

pending completion
#186

push

github

f3l1x
Composer: allow codesniffer

107 of 251 relevant lines covered (42.63%)

0.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.91
/src/SendMailLogger.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Logging;
4

5
use Contributte\Logging\Mailer\IMailer;
6
use Nette\InvalidArgumentException;
7

8
class SendMailLogger extends AbstractLogger
9
{
10

11
        /** @var string */
12
        private $emailSnooze = '2 days';
13

14
        /** @var IMailer */
15
        private $mailer;
16

17
        /** @var string[] */
18
        private $allowedPriority = [ILogger::ERROR, ILogger::EXCEPTION, ILogger::CRITICAL];
19

20
        public function __construct(IMailer $mailer, string $directory)
21
        {
22
                parent::__construct($directory);
1✔
23
                $this->mailer = $mailer;
1✔
24
        }
1✔
25

26
        /**
27
         * @param string[] $allowedPriority
28
         */
29
        public function setAllowedPriority(array $allowedPriority): void
30
        {
31
                $this->allowedPriority = $allowedPriority;
×
32
        }
×
33

34
        public function setEmailSnooze(string $emailSnooze): void
35
        {
36
                $this->emailSnooze = $emailSnooze;
1✔
37
        }
1✔
38

39
        public function setMailer(IMailer $mailer): void
40
        {
41
                $this->mailer = $mailer;
×
42
        }
×
43

44
        /**
45
         * @param mixed $message
46
         */
47
        public function log($message, string $priority = ILogger::INFO): void
48
        {
49
                if (!in_array($priority, $this->allowedPriority, true)) {
1✔
50
                        return;
×
51
                }
52

53
                if (is_numeric($this->emailSnooze)) {
1✔
54
                        $snooze = (int) $this->emailSnooze;
1✔
55
                } else {
56
                        $strtotime = @strtotime($this->emailSnooze);
1✔
57

58
                        if ($strtotime === false) {
1✔
59
                                throw new InvalidArgumentException('Email snooze was not parsed');
×
60
                        }
61

62
                        $snooze = $strtotime - time();
1✔
63
                }
64

65
                $filemtime = @filemtime($this->directory . '/email-sent');
1✔
66

67
                if ($filemtime === false) {
1✔
68
                        $filemtime = 0;
1✔
69
                }
70

71
                if ($filemtime + $snooze < time() && (bool) @file_put_contents($this->directory . '/email-sent', 'sent')
1✔
72
                ) {
73
                        $this->mailer->send($message);
1✔
74
                }
75
        }
1✔
76

77
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc