LaravelBitpayTrait::authenticate()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 1
nop 0
1
<?php
2
3
namespace Vrajroham\LaravelBitpay\Traits;
4
5
use BitPaySDK\Client as BitpayClient;
6
use BitPaySDK\Env;
7
use BitPaySDK\Tokens;
8
use Vrajroham\LaravelBitpay\Exceptions\InvalidConfigurationException;
9
10
trait LaravelBitpayTrait
11
{
12
    public function authenticate()
13
    {
14
        $this->validateAndLoadConfig();
15
16
        $this->client = BitpayClient::create()->withData(
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
            'testnet' == $this->config['network'] ? Env::Test : Env::Prod,
18
            $this->config['private_key'],
19
            new Tokens(
20
                $this->config['token'] //merchant
21
            ),
22
            $this->config['key_storage_password'] //used to decrypt your private key, if encrypted
23
        );
24
    }
25
26
    public function validateAndLoadConfig()
27
    {
28
        $config = config('laravel-bitpay');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $config = /** @scrutinizer ignore-call */ config('laravel-bitpay');
Loading history...
29
30
        if ('livenet' != $config['network'] && 'testnet' != $config['network']) {
31
            throw InvalidConfigurationException::invalidNetworkName();
32
        }
33
34
        if (!class_exists($config['key_storage'])) {
35
            throw InvalidConfigurationException::invalidStorageClass();
36
        }
37
38
        if ('' === trim($config['key_storage_password'])) {
39
            throw InvalidConfigurationException::invalidOrEmptyPassword();
40
        }
41
42
        if ('' === trim($config['token'])) {
43
            throw InvalidConfigurationException::emptyToken();
44
        }
45
46
        $this->config = $config;
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
    }
48
}
49