Passed
Branch master (ab49b6)
by Carlos C
01:55
created

Finkok::settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
 */
42
class Finkok
43
{
44
    /** @var array<array> */
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
    ];
87
88
    /** @var FinkokSettings */
89
    private $settings;
90
91 7
    public function __construct(FinkokSettings $factory)
92
    {
93 7
        $this->settings = $factory;
94 7
    }
95
96 1
    public function settings(): FinkokSettings
97
    {
98 1
        return $this->settings;
99
    }
100
101
    /**
102
     * @param string $name
103
     * @param array<mixed> $arguments
104
     * @return mixed
105
     */
106 4
    public function __call(string $name, array $arguments)
107
    {
108 4
        if (array_key_exists($name, static::SERVICES_MAP)) {
109 3
            $command = $this->checkCommand($name, $arguments[0] ?? null);
110 2
            $service = $this->createService($name);
111 2
            return $this->executeService($name, $service, $command);
112
        }
113 1
        throw new BadMethodCallException(sprintf('Helper %s is not registered', $name));
114
    }
115
116
    /**
117
     * @param string $method
118
     * @param mixed $command
119
     * @return object|null
120
     */
121 3
    protected function checkCommand(string $method, $command): ?object
122
    {
123 3
        $expected = static::SERVICES_MAP[$method][1];
124 3
        if ('' === $expected) {
125
            return null;
126
        }
127 3
        if (! is_a($command, $expected)) {
128 1
            $type = (is_object($command)) ? get_class($command) : gettype($command);
129 1
            throw new InvalidArgumentException(
130 1
                sprintf('Call %s::%s expect %s but received %s', static::class, $method, $expected, $type)
131
            );
132
        }
133 2
        return $command;
134
    }
135
136
    /**
137
     * @param string $method
138
     * @return object
139
     */
140 2
    protected function createService(string $method): object
141
    {
142 2
        $serviceClass = static::SERVICES_MAP[$method][0];
143 2
        return new $serviceClass($this->settings);
144
    }
145
146
    /**
147
     * @param string $method
148
     * @param object $service
149
     * @param object|null $command
150
     * @return mixed
151
     */
152 2
    protected function executeService(string $method, object $service, ?object $command)
153
    {
154 2
        $method = static::SERVICES_MAP[$method][2] ?? $method;
155 2
        if (! is_callable([$service, $method])) {
156 1
            throw new BadMethodCallException(
157 1
                sprintf('The service %s does not have a method %s', get_class($service), $method)
158
            );
159
        }
160 1
        return $service->{$method}($command);
161
    }
162
}
163