Completed
Push — master ( 9820cd...10c8f9 )
by Semyon
01:37
created

ApnServiceProvider::authProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 20
cp 0
rs 9.552
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace SemyonChetvertnyh\ApnNotificationChannel;
4
5
use Illuminate\Notifications\ChannelManager;
6
use Illuminate\Support\ServiceProvider;
7
use InvalidArgumentException;
8
use Pushok\AuthProvider\Certificate;
9
use Pushok\AuthProvider\Token;
10
use Pushok\Client;
11
12
class ApnServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * Bootstrap the application services.
16
     */
17
    public function boot()
18
    {
19
        $this->app->bind(Client::class, function ($app) {
20
            $config = $app['config']->get('broadcasting.connections.apn');
21
22
            return new Client(
23
                $this->authProvider($config), $config['is_production']
24
            );
25
        });
26
27
        $this->app->make(ChannelManager::class)->extend('apn', function () {
28
            return new ApnChannel(
29
                $this->app->make(Client::class)
30
            );
31
        });
32
    }
33
34
    /**
35
     * Determine and get the auth provider.
36
     *
37
     * @param  array  $config
38
     * @return \Pushok\AuthProviderInterface
39
     */
40
    protected function authProvider($config)
41
    {
42
        $config['driver'] = $config['driver'] ?? 'jwt';
43
44
        if ($config['driver'] === 'jwt') {
45
            return Token::create([
46
                'key_id' => $config['key_id'],
47
                'team_id' => $config['team_id'],
48
                'app_bundle_id' => $config['app_bundle_id'],
49
                'private_key_path' => $config['private_key_path'],
50
                'private_key_secret' => $config['private_key_secret'],
51
            ]);
52
        }
53
54
        if ($config['driver'] === 'certificate') {
55
            return Certificate::create([
56
                'certificate_path' => $config['certificate_path'],
57
                'certificate_secret' => $config['certificate_secret'],
58
            ]);
59
        }
60
61
        throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
62
    }
63
}
64