1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpCfdi\Finkok; |
6
|
|
|
|
7
|
|
|
use BadMethodCallException; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use PhpCfdi\Finkok\Services\Cancel; |
10
|
|
|
use PhpCfdi\Finkok\Services\Manifest; |
11
|
|
|
use PhpCfdi\Finkok\Services\Registration; |
12
|
|
|
use PhpCfdi\Finkok\Services\Stamping; |
13
|
|
|
use PhpCfdi\Finkok\Services\Utilities; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Helper class to invoke execute Finkok commands and get the result |
17
|
|
|
* |
18
|
|
|
* @method Stamping\StampingResult stamp(Stamping\StampingCommand $command) |
19
|
|
|
* @method Stamping\StampingResult quickstamp(Stamping\StampingCommand $command) |
20
|
|
|
* @method Stamping\StampingResult stamped(Stamping\StampingCommand $command) |
21
|
|
|
* @method Stamping\QueryPendingResult stampQueryPending(Stamping\QueryPendingCommand $command) |
22
|
|
|
* @method Cancel\CancelSignatureResult cancelSignature(Cancel\CancelSignatureCommand $command) |
23
|
|
|
* @method Cancel\GetPendingResult getPendingToCancel(Cancel\GetPendingCommand $command) |
24
|
|
|
* @method Cancel\GetReceiptResult getCancelReceipt(Cancel\GetReceiptResult $command) |
25
|
|
|
* @method Cancel\GetSatStatusResult getSatStatus(Cancel\GetSatStatusCommand $command) |
26
|
|
|
* @method Cancel\GetRelatedSignatureResult getRelatedSignature(Cancel\GetRelatedSignatureCommand $command) |
27
|
|
|
* @method Cancel\AcceptRejectSignatureResult acceptRejectSignature(Cancel\AcceptRejectSignatureCommand $command) |
28
|
|
|
* @method Utilities\DatetimeResult datetime(Utilities\DatetimeCommand $command) |
29
|
|
|
* @method Utilities\DownloadXmlResult downloadXml(Utilities\DownloadXmlCommand $command) |
30
|
|
|
* @method Utilities\ReportCreditResult reportCredit(Utilities\ReportCreditCommand $command) |
31
|
|
|
* @method Utilities\ReportTotalResult reportTotal(Utilities\ReportTotalCommand $command) |
32
|
|
|
* @method Utilities\ReportUuidResult reportUuid(Utilities\ReportUuidCommand $command) |
33
|
|
|
* @method Manifest\GetContractsResult getContracts(Manifest\GetContractsCommand $command) |
34
|
|
|
* @method Manifest\SignContractsResult signContracts(Manifest\SignContractsCommand $command) |
35
|
|
|
* @method Manifest\GetSignedContractsResult getSignedContracts(Manifest\GetSignedContractsCommand $command) |
36
|
|
|
* @method Registration\AddResult registrationAdd(Registration\AddCommand $command) |
37
|
|
|
* @method Registration\AssignResult registrationAssign(Registration\AssignCommand $command) |
38
|
|
|
* @method Registration\SwitchResult registrationSwitch(Registration\SwitchCommand $command) |
39
|
|
|
* @method Registration\EditResult registrationEdit(Registration\EditCommand $command) |
40
|
|
|
* @method Registration\ObtainResult registrationObtain(Registration\ObtainCommand $command) |
41
|
|
|
* @method Registration\ObtainCustomersResult registrationCustomers(Registration\ObtainCustomersCommand $command) |
42
|
|
|
*/ |
43
|
|
|
class Finkok |
44
|
|
|
{ |
45
|
|
|
protected const SERVICES_MAP = [ |
46
|
|
|
'stamp' => [Stamping\StampService::class, Stamping\StampingCommand::class], |
47
|
|
|
'quickstamp' => [Stamping\QuickStampService::class, Stamping\StampingCommand::class], |
48
|
|
|
'stamped' => [Stamping\StampedService::class, Stamping\StampingCommand::class], |
49
|
|
|
'stampQueryPending' => [ |
50
|
|
|
Stamping\QueryPendingService::class, |
51
|
|
|
Stamping\QueryPendingCommand::class, |
52
|
|
|
'queryPending', // override method name on service |
53
|
|
|
], |
54
|
|
|
'cancelSignature' => [Cancel\CancelSignatureService::class, Cancel\CancelSignatureCommand::class], |
55
|
|
|
'getPendingToCancel' => [Cancel\GetPendingService::class, Cancel\GetPendingCommand::class, 'obtainPending'], |
56
|
|
|
'getCancelReceipt' => [Cancel\GetReceiptService::class, Cancel\GetReceiptResult::class, 'download'], |
57
|
|
|
'getSatStatus' => [Cancel\GetSatStatusService::class, Cancel\GetSatStatusCommand::class, 'query'], |
58
|
|
|
'getRelatedSignature' => [ |
59
|
|
|
Cancel\GetRelatedSignatureService::class, |
60
|
|
|
Cancel\GetRelatedSignatureCommand::class, |
61
|
|
|
], |
62
|
|
|
'acceptRejectSignature' => [ |
63
|
|
|
Cancel\AcceptRejectSignatureService::class, |
64
|
|
|
Cancel\AcceptRejectSignatureCommand::class, |
65
|
|
|
], |
66
|
|
|
'datetime' => [Utilities\DatetimeService::class, Utilities\DatetimeCommand::class], |
67
|
|
|
'downloadXml' => [Utilities\DownloadXmlService::class, Utilities\DownloadXmlCommand::class], |
68
|
|
|
'reportCredit' => [Utilities\ReportCreditService::class, Utilities\ReportCreditCommand::class], |
69
|
|
|
'reportTotal' => [Utilities\ReportTotalService::class, Utilities\ReportTotalCommand::class], |
70
|
|
|
'reportUuid' => [Utilities\ReportUuidService::class, Utilities\ReportUuidCommand::class], |
71
|
|
|
'getContracts' => [Manifest\GetContractsService::class, Manifest\GetContractsCommand::class, 'obtainContracts'], |
72
|
|
|
'signContracts' => [ |
73
|
|
|
Manifest\SignContractsService::class, |
74
|
|
|
Manifest\SignContractsCommand::class, |
75
|
|
|
'sendSignedContracts', |
76
|
|
|
], |
77
|
|
|
'getSignedContracts' => [ |
78
|
|
|
Manifest\GetSignedContractsService::class, |
79
|
|
|
Manifest\GetSignedContractsCommand::class, |
80
|
|
|
], |
81
|
|
|
'registrationAdd' => [Registration\AddService::class, Registration\AddCommand::class, 'add'], |
82
|
|
|
'registrationAssign' => [Registration\AssignService::class, Registration\AssignCommand::class, 'assign'], |
83
|
|
|
'registrationSwitch' => [Registration\SwitchService::class, Registration\SwitchCommand::class, 'switch'], |
84
|
|
|
'registrationEdit' => [Registration\EditService::class, Registration\EditCommand::class, 'edit'], |
85
|
|
|
'registrationObtain' => [Registration\ObtainService::class, Registration\ObtainCommand::class, 'obtain'], |
86
|
|
|
'registrationCustomers' => [ |
87
|
|
|
Registration\ObtainCustomersService::class, |
88
|
|
|
Registration\ObtainCustomersCommand::class, |
89
|
|
|
'obtainPage', |
90
|
|
|
], |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
/** @var FinkokSettings */ |
94
|
|
|
private $settings; |
95
|
|
|
|
96
|
8 |
|
public function __construct(FinkokSettings $factory) |
97
|
|
|
{ |
98
|
8 |
|
$this->settings = $factory; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
public function settings(): FinkokSettings |
102
|
|
|
{ |
103
|
1 |
|
return $this->settings; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $name |
108
|
|
|
* @param array<mixed> $arguments |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
5 |
|
public function __call(string $name, array $arguments) |
112
|
|
|
{ |
113
|
5 |
|
if (array_key_exists($name, static::SERVICES_MAP)) { |
114
|
4 |
|
$command = $this->checkCommand($name, $arguments[0] ?? null); |
115
|
3 |
|
$service = $this->createService($name); |
116
|
3 |
|
return $this->executeService($name, $service, $command); |
117
|
|
|
} |
118
|
1 |
|
throw new BadMethodCallException(sprintf('Helper %s is not registered', $name)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $method |
123
|
|
|
* @param mixed $command |
124
|
|
|
* @return object|null |
125
|
|
|
*/ |
126
|
4 |
|
protected function checkCommand(string $method, $command): ?object |
127
|
|
|
{ |
128
|
4 |
|
$expected = static::SERVICES_MAP[$method][1]; |
129
|
4 |
|
if ('' === $expected) { |
130
|
|
|
return null; |
131
|
|
|
} |
132
|
4 |
|
if (! is_object($command) || ! is_a($command, $expected)) { |
133
|
1 |
|
$type = (is_object($command)) ? get_class($command) : gettype($command); |
134
|
1 |
|
throw new InvalidArgumentException( |
135
|
1 |
|
sprintf('Call %s::%s expect %s but received %s', static::class, $method, $expected, $type) |
136
|
1 |
|
); |
137
|
|
|
} |
138
|
3 |
|
return $command; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $method |
143
|
|
|
* @return object |
144
|
|
|
*/ |
145
|
3 |
|
protected function createService(string $method): object |
146
|
|
|
{ |
147
|
3 |
|
$serviceClass = static::SERVICES_MAP[$method][0]; |
148
|
3 |
|
return new $serviceClass($this->settings); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $method |
153
|
|
|
* @param object $service |
154
|
|
|
* @param object|null $command |
155
|
|
|
* @return mixed |
156
|
|
|
*/ |
157
|
3 |
|
protected function executeService(string $method, object $service, ?object $command) |
158
|
|
|
{ |
159
|
3 |
|
$method = static::SERVICES_MAP[$method][2] ?? $method; |
160
|
3 |
|
if (! is_callable([$service, $method])) { |
161
|
1 |
|
throw new BadMethodCallException( |
162
|
1 |
|
sprintf('The service %s does not have a method %s', get_class($service), $method) |
163
|
1 |
|
); |
164
|
|
|
} |
165
|
2 |
|
return $service->{$method}($command); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|