Completed
Push — master ( f3c42b...9653c9 )
by Elf
03:45
created

ApiServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
namespace ElfSundae\Laravel\Api;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\Contracts\Encryption\Encrypter;
7
8
class ApiServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Register the service provider.
12
     *
13
     * @return void
14
     */
15
    public function register()
16
    {
17
        $this->mergeConfigFrom(__DIR__.'/../config/api.php', 'api');
18
19
        $this->registerClient();
20
21
        $this->registerToken();
22
23
        if ($this->app->runningInConsole()) {
24
            $this->registerForConsole();
25
        }
26
    }
27
28
    /**
29
     * Register the Client singleton.
30
     *
31
     * @return void
32
     */
33
    protected function registerClient()
34
    {
35
        $this->app->singleton('api.client', function ($app) {
36
            $config = $app->make('config');
37
38
            $client = new Client(
39
                $app->make(Encrypter::class),
40
                $config->get('api.clients', [])
41
            );
42
43
            $client->setDefaultAppKey($config->get('api.default_client'));
44
45
            return $client;
46
        });
47
48
        $this->app->alias('api.client', Client::class);
49
    }
50
51
    /**
52
     * Register the Token singleton.
53
     *
54
     * @return void
55
     */
56
    protected function registerToken()
57
    {
58
        $this->app->singleton('api.token', function ($app) {
59
            return new Token($app->make(Client::class));
60
        });
61
62
        $this->app->alias('api.token', Token::class);
63
    }
64
65
    /**
66
     * Register for console.
67
     *
68
     * @return void
69
     */
70
    protected function registerForConsole()
71
    {
72
        $this->publishes([
73
            __DIR__.'/../config/api.php' => base_path('config/api.php'),
74
        ], 'laravel-api');
75
76
        $this->commands([
77
            Console\GenerateClientCommand::class,
78
            Console\GenerateTokenCommand::class,
79
        ]);
80
    }
81
}
82