MtnMomoServiceProvider::commandClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 1.0001

Importance

Changes 0
Metric Value
cc 1
eloc 16
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 25
ccs 19
cts 20
cp 0.95
crap 1.0001
rs 9.7333
1
<?php
2
/**
3
 * MtnMomoServiceProvider.
4
 */
5
6
namespace Bmatovu\MtnMomo;
7
8
use Bmatovu\MtnMomo\Console\BootstrapCommand;
9
use Bmatovu\MtnMomo\Console\RegisterIdCommand;
10
use Bmatovu\MtnMomo\Console\RequestSecretCommand;
11
use Bmatovu\MtnMomo\Console\ValidateIdCommand;
12
use GuzzleHttp\Client;
13
use GuzzleHttp\ClientInterface;
14
use GuzzleHttp\HandlerStack;
15
use Illuminate\Support\ServiceProvider;
16
17
/**
18
 * Package service provider.
19
 */
20
class MtnMomoServiceProvider extends ServiceProvider
21
{
22
    /**
23
     * Bootstrap the application services.
24
     *
25
     * @return void
26
     */
27 51
    public function boot()
28
    {
29 51
        if ($this->app->runningInConsole()) {
30 51
            $this->publishes([
31 51
                __DIR__.'/../config/mtn-momo.php' => base_path('config/mtn-momo.php'),
32 51
            ], 'config');
33
34 51
            $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
35
36 51
            $this->commands([
37 51
                BootstrapCommand::class,
38 51
                RegisterIdCommand::class,
39 51
                ValidateIdCommand::class,
40 51
                RequestSecretCommand::class,
41 51
            ]);
42
        }
43
    }
44
45
    /**
46
     * Register the application services.
47
     *
48
     * @return void
49
     */
50 51
    public function register()
51
    {
52 51
        $this->mergeConfigFrom(__DIR__.'/../config/mtn-momo.php', 'mtn-momo');
53
54 51
        $this->app->bind(ClientInterface::class, function () {
55 25
            return $this->commandClient();
56 51
        });
57
    }
58
59
    /**
60
     * Create command's concrete client.
61
     *
62
     * @throws \Exception
63
     *
64
     * @return \GuzzleHttp\ClientInterface
65
     */
66 25
    protected function commandClient()
67
    {
68 25
        $handlerStack = HandlerStack::create();
69
70 25
        $handlerStack = append_log_middleware($handlerStack);
71
72 25
        $product = $this->app['config']->get('mtn-momo.product');
73
74 25
        $options = array_merge([
75 25
            'handler' => $handlerStack,
76 25
            'progress' => function () {
77
                echo '. ';
78 25
            },
79 25
            'base_uri' => $this->app['config']->get('mtn-momo.api.base_uri'),
80 25
            'headers' => [
81 25
                'Accept' => 'application/json',
82 25
                'Content-Type' => 'application/json',
83 25
                'Ocp-Apim-Subscription-Key' => $this->app['config']->get("mtn-momo.products.{$product}.key"),
84 25
            ],
85 25
            'json' => [
86 25
                'non-empty-body',
87 25
            ],
88 25
        ], (array) $this->app['config']->get('mtn-momo.guzzle.options'));
89
90 25
        return new Client($options);
91
    }
92
}
93