Completed
Push — master ( a02954...8fdef2 )
by Elf
03:38
created

ApiServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 104
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 10 2
A registerClient() 0 17 1
A registerToken() 0 10 1
A registerForConsole() 0 8 1
A aliasFacade() 0 8 2
A provides() 0 4 1
1
<?php
2
3
namespace ElfSundae\Laravel\Api;
4
5
use Illuminate\Support\ServiceProvider;
6
use ElfSundae\Laravel\Api\Console\ConsoleServiceProvider;
7
8
class ApiServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Indicates if loading of the provider is deferred.
12
     *
13
     * @var bool
14
     */
15
    protected $defer = true;
16
17
    /**
18
     * Register the service provider.
19
     *
20
     * @return void
21
     */
22
    public function register()
23
    {
24
        $this->registerClient();
25
26
        $this->registerToken();
27
28
        if ($this->app->runningInConsole()) {
29
            $this->registerForConsole();
30
        }
31
    }
32
33
    /**
34
     * Register the Client singleton.
35
     *
36
     * @return void
37
     */
38
    protected function registerClient()
39
    {
40
        $this->app->singleton('api.client', function ($app) {
41
            $config = $app->make('config');
42
43
            $client = new Client(
44
                $app->make('encrypter'),
45
                $config->get('api.clients', [])
46
            );
47
48
            return $client->setDefaultAppKey($config->get('api.default_client'));
49
        });
50
51
        $this->app->alias('api.client', Client::class);
52
53
        $this->aliasFacade('ApiClient', Facades\ApiClient::class);
54
    }
55
56
    /**
57
     * Register the Token singleton.
58
     *
59
     * @return void
60
     */
61
    protected function registerToken()
62
    {
63
        $this->app->singleton('api.token', function ($app) {
64
            return new Token($app->make('api.client'));
65
        });
66
67
        $this->app->alias('api.token', Token::class);
68
69
        $this->aliasFacade('ApiToken', Facades\ApiToken::class);
70
    }
71
72
    /**
73
     * Register for console.
74
     *
75
     * @return void
76
     */
77
    protected function registerForConsole()
78
    {
79
        $this->publishes([
80
            __DIR__.'/../config/api.php' => config_path('api.php')
81
        ], 'laravel-api');
82
83
        $this->app->register(ConsoleServiceProvider::class);
84
    }
85
86
    /**
87
     * Create alias for the facade.
88
     *
89
     * @param  string  $facade
90
     * @param  string  $class
91
     * @return void
92
     */
93
    protected function aliasFacade($facade, $class)
94
    {
95
        if (class_exists('Illuminate\Foundation\AliasLoader')) {
96
            \Illuminate\Foundation\AliasLoader::getInstance()->alias($facade, $class);
97
        } else {
98
            class_alias($class, $facade);
99
        }
100
    }
101
102
    /**
103
     * Get the services provided by the provider.
104
     *
105
     * @return string[]
106
     */
107
    public function provides()
108
    {
109
        return ['api.client', 'api.token'];
110
    }
111
}
112