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
|
|
|
|