Completed
Push — master ( 76299f...8e6e1c )
by Sébastien
10:27
created

VivaPaymentsServiceProvider::isDeferred()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
     * Register the application services.
12
     *
13
     * @return void
14
     */
15
    public function register()
16
    {
17
        $this->mergeConfigFrom(
18
            __DIR__.'/../config/services.php',
19
            'services'
20
        );
21
22
        $this->app->singleton(Client::class, function ($app) {
23
            return new Client($this->buildGuzzleClient($app));
24
        });
25
    }
26
27
    /**
28
     * Build the Guzzlehttp client.
29
     *
30
     * @param  \Illuminate\Contracts\Foundation\Application $app
31
     * @return \GuzzleHttp\Client
32
     */
33
    protected function buildGuzzleClient($app)
34
    {
35
        $config = $app['config']->get('services.viva');
36
37
        return new GuzzleClient([
38
            'base_uri' => $this->getUrl($config['environment']),
39
            'curl' => $this->curlDoesntUseNss()
40
                ? [CURLOPT_SSL_CIPHER_LIST => 'TLSv1']
41
                : [],
42
            \GuzzleHttp\RequestOptions::AUTH => [
43
                $config['merchant_id'],
44
                $config['api_key'],
45
            ],
46
        ]);
47
    }
48
49
    /**
50
     * Check if cURL doens't use NSS.
51
     *
52
     * @return bool
53
     */
54
    protected function curlDoesntUseNss()
55
    {
56
        $curl = curl_version();
57
58
        return ! preg_match('/NSS/', $curl['ssl_version']);
59
    }
60
61
    /**
62
     * Get the URL based on the environment.
63
     *
64
     * @param  string $environment
65
     * @return string
66
     */
67
    protected function getUrl($environment)
68
    {
69
        if ($environment === 'production') {
70
            return Client::PRODUCTION_URL;
71
        }
72
73
        if ($environment === 'demo') {
74
            return Client::DEMO_URL;
75
        }
76
77
        throw new \InvalidArgumentException('The Viva Payments environment must be demo or production.');
78
    }
79
80
    /**
81
     * Determine if the provider is deferred.
82
     *
83
     * @return bool
84
     */
85
    public function isDeferred()
86
    {
87
        return true;
88
    }
89
90
    /**
91
     * Get the services provided by the provider.
92
     *
93
     * @return array
94
     */
95
    public function provides()
96
    {
97
        return [Client::class];
98
    }
99
}
100