Passed
Pull Request — master (#50)
by Ronan
09:06
created

MailProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 25
c 1
b 0
f 0
dl 0
loc 42
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 37 4
1
<?php
2
3
namespace App\Mail;
4
5
use App\Mail\Helper;
6
use Ronanchilvers\Container\Container;
7
use Ronanchilvers\Container\ServiceProviderInterface;
8
use Slim\Views\Twig;
9
use Swift_Mailer;
10
use Swift_SmtpTransport;
11
12
/**
13
 * Provider for mail services
14
 *
15
 * @author Ronan Chilvers <[email protected]>
16
 */
17
class MailProvider implements ServiceProviderInterface
18
{
19
    /**
20
     * @author Ronan Chilvers <[email protected]>
21
     */
22
    public function register(Container $container)
23
    {
24
        $container->set('mailer_settings', []);
25
26
        $container->set('swift_mailer', function ($c) {
27
            $config = $c->get('mailer_settings')['transport'];
28
            $transport = new Swift_SmtpTransport(
29
                $config['host'],
30
                $config['port']
31
            );
32
            if (isset($config['username'], $config['password'])) {
33
                $transport->setUsername(
34
                    $config['username']
35
                );
36
                $transport->setPassword(
37
                    $config['password']
38
                );
39
            }
40
            if (isset($config['tls']) && true == $config['tls']) {
41
                $transport->setEncryption(
42
                    'tls'
43
                );
44
            }
45
46
            return new Swift_Mailer(
47
                $transport
48
            );
49
        });
50
        $container->set(Helper::class, function ($c) {
51
            $config      = $c->get('mailer_settings')['options'];
52
            $swiftMailer = $c->get('swift_mailer');
53
            $twig        = $c->get(Twig::class)->getEnvironment();
54
55
            return new Helper(
56
                $config,
57
                $swiftMailer,
58
                $twig
59
            );
60
        });
61
    }
62
}
63