Passed
Push — master ( 89f24e...8c4dad )
by Sylvain
08:05
created

MailFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 35
ccs 18
cts 18
cp 1
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 30 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Log\Writer;
6
7
use Ecodev\Felix\Log\EventCompleter;
8
use Ecodev\Felix\Log\Filter\NoMail;
9
use Ecodev\Felix\Log\Formatter\Extras;
10
use Interop\Container\ContainerInterface;
11
use Laminas\Log\Logger;
12
use Laminas\Log\Writer\WriterInterface;
13
use Laminas\Mail\Message;
14
use Laminas\Mail\Transport\TransportInterface;
15
use Laminas\ServiceManager\Factory\FactoryInterface;
16
17
final class MailFactory implements FactoryInterface
18
{
19
    /**
20
     * @param string $requestedName
21
     */
22 5
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): ?WriterInterface
23
    {
24 5
        $config = $container->get('config');
25
26 5
        $emailsTo = $config['log']['emails'] ?? [];
27 5
        if (!$emailsTo) {
28 2
            return null;
29
        }
30
31 3
        $hostname = $config['hostname'];
32 3
        $emailFrom = $config['email']['from'];
33
34 3
        $mail = new Message();
35 3
        $mail->setFrom($emailFrom)
36 3
            ->addTo($emailsTo);
37
38 3
        $transport = $container->get(TransportInterface::class);
39 3
        $eventCompleter = $container->get(EventCompleter::class);
40 3
        $writerEmail = new Mail($mail, $transport, $eventCompleter);
41 3
        $writerEmail->setFormatter($container->get(Extras::class));
42
43
        // Set subject text for use; summary of number of errors is appended to the
44
        // subject line before sending the message.
45 3
        $writerEmail->setSubjectPrependText('Errors on ' . $hostname);
46
47
        // Only email error level entries and higher
48 3
        $writerEmail->addFilter(Logger::ERR);
49 3
        $writerEmail->addFilter(new NoMail());
50
51 3
        return $writerEmail;
52
    }
53
}
54