HeloLaravelServiceProvider::boot()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
3
namespace BeyondCode\HeloLaravel;
4
5
use Illuminate\Contracts\Mail\Mailer as MailerContract;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Facades\Mail;
8
use Illuminate\Support\Facades\View;
9
use Illuminate\Support\ServiceProvider;
10
use Illuminate\Support\Str;
11
use Swift_Mailer;
12
13
class HeloLaravelServiceProvider extends ServiceProvider
14
{
15
    /**
16
     * Bootstrap the application services.
17
     */
18
    public function boot()
19
    {
20
        if ($this->app->runningUnitTests() || !$this->app['config']['helo.is_enabled']) {
21
            return;
22
        }
23
24
        $instance = app()->make(Mailer::class);
25
26
        Mail::swap($instance);
27
28
        app()->instance(MailerContract::class, $instance);
29
        
30
        if ($this->app->runningInConsole()) {
31
            View::addNamespace('helo', __DIR__ . '/../resources/views');
32
        }
33
    }
34
35
    /**
36
     * Register the application services.
37
     */
38
    public function register()
39
    {
40
        if ($this->app->runningInConsole()) {
41
            $this->commands([
42
                TestMailCommand::class,
43
            ]);
44
            
45
            $this->publishes([
46
                __DIR__.'/../config/helo.php' => base_path('config/helo.php'),
47
            ], 'config');
48
        }
49
50
        $this->mergeConfigFrom(__DIR__.'/../config/helo.php', 'helo');
51
52
        $this->app->singleton(Mailer::class, function ($app) {
53
            if (version_compare($app->version(), '7.0.0', '<')) {
54
                return $this->createLaravel6Mailer($app);
55
            }
56
57
            return $this->createLaravel7Mailer($app);
58
        });
59
    }
60
61
    protected function createLaravel6Mailer($app)
62
    {
63
        $config = $this->getConfig();
64
65
        // Once we have create the mailer instance, we will set a container instance
66
        // on the mailer. This allows us to resolve mailer classes via containers
67
        // for maximum testability on said classes instead of passing Closures.
68
        $mailer = new Mailer(
69
            $app['view'], $app['swift.mailer'], $app['events']
70
        );
71
72
        if ($app->bound('queue')) {
73
            $mailer->setQueue($app['queue']);
74
        }
75
76
        // Next we will set all of the global addresses on this mailer, which allows
77
        // for easy unification of all "from" addresses as well as easy debugging
78
        // of sent messages since they get be sent into a single email address.
79
        foreach (['from', 'reply_to', 'to'] as $type) {
80
            $this->setGlobalAddress($mailer, $config, $type);
81
        }
82
83
        return $mailer;
84
    }
85
86
    protected function createLaravel7Mailer($app)
87
    {
88
        $defaultDriver = $app['mail.manager']->getDefaultDriver();
89
        $config = $this->getConfig($defaultDriver);
90
91
        // Laravel 7 no longer bindes the swift.mailer:
92
        $swiftMailer = new Swift_Mailer($app['mail.manager']->createTransport($config));
93
94
        // Once we have create the mailer instance, we will set a container instance
95
        // on the mailer. This allows us to resolve mailer classes via containers
96
        // for maximum testability on said classes instead of passing Closures.
97
        $mailer = new Laravel7Mailer(
98
            'smtp', $app['view'], $swiftMailer, $app['events']
99
        );
100
101
        if ($app->bound('queue')) {
102
            $mailer->setQueue($app['queue']);
103
        }
104
105
        // Next we will set all of the global addresses on this mailer, which allows
106
        // for easy unification of all "from" addresses as well as easy debugging
107
        // of sent messages since they get be sent into a single email address.
108
        foreach (['from', 'reply_to', 'to', 'return_path'] as $type) {
109
            $this->setGlobalAddress($mailer, $config, $type);
110
        }
111
112
        return $mailer;
113
    }
114
115
    protected function getConfig($name = 'smtp')
116
    {
117
        return $this->app['config']['mail.driver']
118
            ? $this->app['config']['mail']
119
            : $this->app['config']["mail.mailers.{$name}"];
120
    }
121
122
    /**
123
     * Set a global address on the mailer by type.
124
     *
125
     * @param \Illuminate\Mail\Mailer $mailer
126
     * @param array $config
127
     * @param string $type
128
     * @return void
129
     */
130
    protected function setGlobalAddress($mailer, array $config, $type)
131
    {
132
        if (version_compare(app()->version(), '7.0.0', '<')) {
133
            $address = Arr::get($config, $type);
134
        } else {
135
            $address = Arr::get($config, $type, $this->app['config']['mail.'.$type]);
136
        }
137
138
        if (is_array($address) && isset($address['address'])) {
139
            $mailer->{'always' . Str::studly($type)}($address['address'], $address['name']);
140
        }
141
    }
142
}
143