MailServiceProvider   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 115
Duplicated Lines 5.22 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 11
c 7
b 0
f 0
lcom 1
cbo 5
dl 6
loc 115
ccs 0
cts 58
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 6 1
B registerMailer() 6 32 5
A setMailerDependencies() 0 6 1
A registerSwiftMailer() 0 10 1
A registerSwiftMailerManager() 0 9 1
A provides() 0 9 1
A registerSwiftTransport() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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