Completed
Push — master ( 8aa23e...040690 )
by Sébastien
06:56
created

VivaPaymentsServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 10 1
A buildGuzzleClient() 0 15 2
A curlDoesntUseNss() 0 6 1
A getUrl() 0 12 3
A provides() 0 4 1
1
<?php
2
3
namespace Sebdesign\VivaPayments;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
use Illuminate\Support\ServiceProvider;
7
8
class VivaPaymentsServiceProvider 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 application services.
19
     *
20
     * @return void
21
     */
22
    public function register()
23
    {
24
        $this->mergeConfigFrom(
25
            __DIR__.'/../config/services.php', 'services'
26
        );
27
28
        $this->app->singleton(Client::class, function ($app) {
29
            return new Client($this->buildGuzzleClient($app));
30
        });
31
    }
32
33
    /**
34
     * Build the Guzzlehttp client.
35
     *
36
     * @param  \Illuminate\Contracts\Foundation\Application $app
37
     * @return GuzzleHttp\Client
38
     */
39
    protected function buildGuzzleClient($app)
40
    {
41
        $config = $app['config']->get('services.viva');
42
43
        return new GuzzleClient([
44
            'base_uri' => $this->getUrl($config['environment']),
45
            'curl' => $this->curlDoesntUseNss()
46
                ? [CURLOPT_SSL_CIPHER_LIST => 'TLSv1']
47
                : [],
48
            \GuzzleHttp\RequestOptions::AUTH => [
49
                $config['merchant_id'],
50
                $config['api_key'],
51
            ],
52
        ]);
53
    }
54
55
    /**
56
     * Check if cURL doens't use NSS.
57
     *
58
     * @return bool
59
     */
60
    protected function curlDoesntUseNss()
61
    {
62
        $curl = curl_version();
63
64
        return ! preg_match('/NSS/', $curl['ssl_version']);
65
    }
66
67
    /**
68
     * Get the URL based on the environment.
69
     *
70
     * @param  string $environment
71
     * @return string
72
     */
73
    protected function getUrl($environment)
74
    {
75
        if ($environment === 'production') {
76
            return Client::PRODUCTION_URL;
77
        }
78
79
        if ($environment === 'demo') {
80
            return Client::DEMO_URL;
81
        }
82
83
        throw new \InvalidArgumentException('The Viva Payments environment must be demo or production.');
84
    }
85
86
    /**
87
     * Get the services provided by the provider.
88
     *
89
     * @return array
90
     */
91
    public function provides()
92
    {
93
        return [Client::class];
94
    }
95
}
96