Failed Conditions
Push — master ( 5f5e96...daf37e )
by Adrien
03:02
created

MailerHandlerFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 27 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Log\Handler;
6
7
use Ecodev\Felix\Log\RecordCompleter;
8
use Laminas\ServiceManager\Factory\FactoryInterface;
9
use Psr\Container\ContainerInterface;
10
use Symfony\Component\Mailer\Transport\TransportInterface;
11
use Symfony\Component\Mime\Email;
12
13
final class MailerHandlerFactory implements FactoryInterface
14
{
15
    /**
16
     * @param string $requestedName
17
     */
18 5
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): ?MailerHandler
19
    {
20
        /** @var array $config */
21 5
        $config = $container->get('config');
22
23 5
        $emailsTo = $config['log']['emails'] ?? [];
24 5
        if (!$emailsTo) {
25 2
            return null;
26
        }
27
28 3
        $hostname = $config['hostname'];
29 3
        $emailFrom = $config['email']['from'];
30
31 3
        $email = new Email();
32 3
        $email->subject("%level_name% on $hostname: %extra.login%: %message%");
33 3
        $email->addFrom(...(array) $emailFrom);
34 3
        $email->addTo(...(array) $emailsTo);
35
36
        /** @var TransportInterface $transport */
37 3
        $transport = $container->get(TransportInterface::class);
38
        /** @var RecordCompleter $recordCompleter */
39 3
        $recordCompleter = $container->get(RecordCompleter::class);
40
41 3
        $mailerHandler = new MailerHandler($transport, $email);
42 3
        $mailerHandler->pushProcessor($recordCompleter);
43
44 3
        return $mailerHandler;
45
    }
46
}
47