LaravelFirebaseServiceProvider::boot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Richdynamix\LaravelFirebase;
4
5
use Illuminate\Support\ServiceProvider;
6
use Kreait\Firebase\ServiceAccount;
7
use Richdynamix\LaravelFirebase\Exceptions\InvalidConfig;
8
9
class LaravelFirebaseServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Bootstrap the application services.
13
     */
14
    public function boot()
15
    {
16
        if ($this->app->runningInConsole()) {
17
            $this->publishes([
18
                __DIR__.'/../config/config.php' => config_path('firebase.php'),
19
            ], 'config');
20
        }
21
    }
22
23
    /**
24
     * Register the application services.
25
     */
26
    public function register()
27
    {
28
        // Automatically apply the package configuration
29
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'firebase');
30
31
        $this->app->singleton(FirebaseFactory::class, function () {
32
            $config = config('firebase');
33
            $this->guardAgainstInvalidConfiguration($config);
34
35
            $serviceAccount = ServiceAccount::fromJsonFile(
36
                $config['service_account_json']
37
            );
38
39
            return FirebaseFactory::create($serviceAccount, $config);
40
        });
41
    }
42
43
    /**
44
     * @param  array|null  $config
45
     *
46
     * @throws InvalidConfig
47
     */
48
    protected function guardAgainstInvalidConfiguration(array $config = null)
49
    {
50
        if (!file_exists($config['service_account_json'])) {
51
            throw InvalidConfig::credentialsJsonMissing(
52
                $config['service_account_json']
53
            );
54
        }
55
    }
56
}
57