Completed
Push — master ( a1a6a3...9324c5 )
by Jean C.
04:28
created

MoipServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 8
c 8
b 0
f 0
lcom 1
cbo 4
dl 0
loc 73
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
A register() 0 6 1
A provides() 0 4 1
A handleConfigs() 0 12 2
A isLumen() 0 4 1
A getHomologated() 0 4 2
1
<?php
2
3
namespace Artesaos\Moip\Providers;
4
5
use Artesaos\Moip\Moip;
6
use Illuminate\Support\ServiceProvider;
7
use Moip\Moip as Api;
8
use Moip\MoipBasicAuth;
9
10
/**
11
 * Class MoipServiceProvider.
12
 *
13
 * @package Artesaos\Moip\Providers
14
 */
15
class MoipServiceProvider extends ServiceProvider
16
{
17
    /**
18
     * Indicates if loading of the provider is deferred.
19
     *
20
     * @var bool
21
     */
22
    protected $defer = true;
23
24
    /**
25
     * Bootstrap the application events.
26
     */
27
    public function boot()
28
    {
29
        $this->handleConfigs();
30
    }
31
32
    /**
33
     * Register the service provider.
34
     */
35
    public function register()
36
    {
37
        $this->app->singleton('moip', function(){
38
            return new Moip(new Api(new MoipBasicAuth($this->app->make('config')->get('moip.credentials.token'), $this->app->make('config')->get('moip.credentials.key')), $this->getHomologated()));
39
        });
40
    }
41
42
    /**
43
     * Get the services provided by the provider.
44
     *
45
     * @return array
46
     */
47
    public function provides()
48
    {
49
        return ['moip'];
50
    }
51
52
    /**
53
     * Publishes and Merge configs.
54
     */
55
    public function handleConfigs()
56
    {
57
        $config_file = __DIR__.'/../../config/moip.php';
58
59
        if ($this->isLumen()) {
60
            $this->app->configure('moip');
61
        } else {
62
            $this->publishes([$config_file => config_path('moip.php')]);
63
        }
64
65
        $this->mergeConfigFrom($config_file, 'moip');
66
    }
67
68
    /**
69
     * Checks if the application is Lumen.
70
     *
71
     * @return bool
72
     */
73
    private function isLumen()
74
    {
75
        return true === str_contains($this->app->version(), 'Lumen');
76
    }
77
78
    /**
79
     * Get endpoint of request.
80
     *
81
     * @return string
82
     */
83
    private function getHomologated()
84
    {
85
        return $this->app->make('config')->get('moip.homologated') === true ? Api::ENDPOINT_PRODUCTION : Api::ENDPOINT_SANDBOX;
86
    }
87
}
88