Passed
Pull Request — master (#11)
by Carlos C
01:51
created

FinkokSettings::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\Finkok;
6
7
use PhpCfdi\Finkok\Definitions\Services;
8
use PhpCfdi\Finkok\Exceptions\InvalidArgumentException;
9
10
/**
11
 * @todo Renombrar a FinkokContainer
12
 */
13
class FinkokSettings
14
{
15
    /** @var string */
16
    private $username;
17
18
    /** @var string */
19
    private $password;
20
21
    /** @var FinkokEnvironment */
22
    private $environment;
23
24
    /** @var SoapFactory */
25
    private $soapFactory;
26
27 31
    public function __construct(string $username, string $password, FinkokEnvironment $environment = null)
28
    {
29 31
        if ('' === $username) {
30 1
            throw new InvalidArgumentException('Invalid username');
31
        }
32 30
        if ('' === $password) {
33 1
            throw new InvalidArgumentException('Invalid password');
34
        }
35 29
        $this->username = $username;
36 29
        $this->password = $password;
37 29
        $this->environment = $environment ?? FinkokEnvironment::makeDevelopment();
38 29
        $this->soapFactory = new SoapFactory();
39 29
    }
40
41 26
    public function changeSoapFactory(SoapFactory $soapFactory): void
42
    {
43 26
        $this->soapFactory = $soapFactory;
44 26
    }
45
46 24
    public function username(): string
47
    {
48 24
        return $this->username;
49
    }
50
51 24
    public function password(): string
52
    {
53 24
        return $this->password;
54
    }
55
56 28
    public function environment(): FinkokEnvironment
57
    {
58 28
        return $this->environment;
59
    }
60
61 26
    public function soapFactory(): SoapFactory
62
    {
63 26
        return $this->soapFactory;
64
    }
65
66
    /**
67
     * This method created a configured SoapCaller with wsdlLocation and default options
68
     *
69
     * @param Services $service
70
     * @param string $usernameKey defaults to username, if empty then it will be ommited
71
     * @param string $passwordKey defaults to password, if empty then it will be ommited
72
     * @return SoapCaller
73
     */
74 26
    public function createCallerForService(
75
        Services $service,
76
        string $usernameKey = 'username',
77
        string $passwordKey = 'password'
78
    ): SoapCaller {
79 26
        $wsdlLocation = $this->environment()->endpoint($service);
80 26
        $credentials = array_merge(
81 26
            ('' !== $usernameKey) ? [$usernameKey => $this->username()] : [],
82 26
            ('' !== $passwordKey) ? [$passwordKey => $this->password()] : []
83
        );
84 26
        return $this->soapFactory()->createSoapCaller($wsdlLocation, $credentials);
85
    }
86
}
87