Completed
Pull Request — master (#17)
by Adam
02:17
created

extendTransportManager()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 8
nc 1
nop 1
1
<?php
2
namespace Sichikawa\LaravelSendgridDriver;
3
4
use GuzzleHttp\Client as HttpClient;
5
use Illuminate\Mail\TransportManager;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\ServiceProvider;
8
use Sichikawa\LaravelSendgridDriver\Transport\SendgridTransport;
9
use Sichikawa\LaravelSendgridDriver\Transport\SendgridV3Transport;
10
11
class SendgridTransportServiceProvider extends ServiceProvider
12
{
13
    /**
14
     * Register the Swift Transport instance.
15
     *
16
     * @return void
17
     */
18
    public function register()
19
    {
20
        $this->app->afterResolving(TransportManager::class, function(TransportManager $manager) {
21
            $this->extendTransportManager($manager);
22
        });
23
    }
24
25
    public function extendTransportManager(TransportManager $manager)
26
    {
27
        $manager->extend('sendgrid', function() {
28
            $config = $this->app['config']->get('services.sendgrid', array());
29
            $client = new HttpClient(Arr::get($config, 'guzzle', []));
30
31
            if (Arr::get($config, 'version') === 'v3') {
32
                $pretend = isset($config['pretend']) ? $config['pretend'] : false;
33
                return new SendgridV3Transport($client, $config['api_key'], $pretend);
34
            }
35
36
            return new SendgridTransport($client, $config['api_key']);
37
        });
38
    }
39
}
40