MailServiceProvider::setMailerDependencies()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace ElfSundae\Multimail;
4
5
use Illuminate\Mail\MailServiceProvider as BaseServiceProvider;
6
7
class MailServiceProvider extends BaseServiceProvider
8
{
9
    /**
10
     * Register the service provider.
11
     */
12
    public function register()
13
    {
14
        $this->registerSwiftMailer();
15
16
        $this->registerMailer();
17
    }
18
19
    /**
20
     * Register the Mailer instance.
21
     */
22
    protected function registerMailer()
23
    {
24
        $this->app->singleton('mailer', function ($app) {
25
            // Once we have create the mailer instance, we will set a container instance
26
            // on the mailer. This allows us to resolve mailer classes via containers
27
            // for maximum testability on said classes instead of passing Closures.
28
            $mailer = new Mailer(
29
                $app['view'], $app['swift.mailer'], $app['events']
30
            );
31
32
            $this->setMailerDependencies($mailer, $app);
33
34
            // If a "from" address is set, we will set it on the mailer so that all mail
35
            // messages sent by the applications will utilize the same "from" address
36
            // on each one, which makes the developer's life a lot more convenient.
37
            $from = $app['config']['mail.from'];
38
39 View Code Duplication
            if (is_array($from) && isset($from['address'])) {
40
                $mailer->alwaysFrom($from['address'], $from['name']);
41
            }
42
43
            $to = $app['config']['mail.to'];
44
45 View Code Duplication
            if (is_array($to) && isset($to['address'])) {
46
                $mailer->alwaysTo($to['address'], $to['name']);
47
            }
48
49
            return $mailer;
50
        });
51
52
        $this->app->alias('mailer', Mailer::class);
53
    }
54
55
    /**
56
     * Set a few dependencies on the mailer instance.
57
     *
58
     * @param  \ElfSundae\Multimail\Mailer  $mailer
59
     * @param  \Illuminate\Foundation\Application  $app
60
     */
61
    protected function setMailerDependencies($mailer, $app)
62
    {
63
        parent::setMailerDependencies($mailer, $app);
64
65
        $mailer->setSwiftMailerManager($app['swift.manager']);
66
    }
67
68
    /**
69
     * Register the Swift Mailer instance.
70
     */
71
    public function registerSwiftMailer()
72
    {
73
        $this->registerSwiftTransport();
74
75
        $this->registerSwiftMailerManager();
76
77
        $this->app->bind('swift.mailer', function ($app) {
78
            return $app['swift.manager']->mailer();
79
        });
80
    }
81
82
    /**
83
     * Register the Swift Transport instance.
84
     */
85
    protected function registerSwiftTransport()
86
    {
87
        $this->app->singleton('swift.transport', function ($app) {
88
            return new TransportManager($app);
89
        });
90
91
        $this->app->alias('swift.transport', TransportManager::class);
92
    }
93
94
    /**
95
     * Register the Swift Mailer Manager instance.
96
     */
97
    protected function registerSwiftMailerManager()
98
    {
99
        $this->app->singleton('swift.manager', function ($app) {
100
            return (new SwiftMailerManager($app))
101
                ->setTransportManager($app['swift.transport']);
102
        });
103
104
        $this->app->alias('swift.manager', SwiftMailerManager::class);
105
    }
106
107
    /**
108
     * Get the services provided by the provider.
109
     *
110
     * @return array
111
     */
112
    public function provides()
113
    {
114
        return array_merge(parent::provides(), [
115
            'swift.manager',
116
            Mailer::class,
117
            SwiftMailerManager::class,
118
            TransportManager::class,
119
        ]);
120
    }
121
}
122