Completed
Pull Request — master (#11)
by Carlos C
03:41
created

Finkok::checkCommand()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 4
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()
29
 * @method Utilities\DatetimeResult datetimePostalCode(Utilities\DatetimeCommand $command)
30
 * @method Utilities\DownloadXmlResult downloadXml(Utilities\DownloadXmlCommand $command)
31
 * @method Utilities\ReportCreditResult reportCredit(Utilities\ReportCreditCommand $command)
32
 * @method Utilities\ReportTotalResult reportTotal(Utilities\ReportTotalCommand $command)
33
 * @method Utilities\ReportUuidResult reportUuid(Utilities\ReportUuidCommand $command)
34
 * @method Manifest\GetContractsResult getContracts(Manifest\GetContractsCommand $command)
35
 * @method Manifest\SignContractsResult signContracts(Manifest\SignContractsCommand $command)
36
 * @method Registration\AddResult registrationAdd(Registration\AddCommand $command)
37
 * @method Registration\AssignResult registrationAssign(Registration\AssignCommand $command)
38
 * @method Registration\EditResult registrationEdit(Registration\EditCommand $command)
39
 * @method Registration\ObtainResult registrationObtain(Registration\ObtainCommand $command)
40
 */
41
class Finkok
42
{
43
    protected const SERVICES_MAP = [
44
        'stamp' => [Stamping\StampService::class, Stamping\StampingCommand::class],
45
        'quickstamp' => [Stamping\QuickStampService::class, Stamping\StampingCommand::class],
46
        'stamped' => [Stamping\StampedService::class, Stamping\StampingCommand::class],
47
        'stampQueryPending' => [
48
            Stamping\QueryPendingService::class,
49
            Stamping\QueryPendingCommand::class,
50
            'queryPending', // override method name on service
51
        ],
52
        'cancelSignature' => [Cancel\CancelSignatureService::class, Cancel\CancelSignatureCommand::class],
53
        'getPendingToCancel' => [Cancel\GetPendingService::class, Cancel\GetPendingCommand::class, 'obtainPending'],
54
        'getCancelReceipt' => [Cancel\GetReceiptService::class, Cancel\GetReceiptResult::class, 'download'],
55
        'getSatStatus' => [Cancel\GetSatStatusService::class, Cancel\GetSatStatusCommand::class, 'query'],
56
        'getRelatedSignature' => [
57
            Cancel\GetRelatedSignatureService::class,
58
            Cancel\GetRelatedSignatureCommand::class,
59
        ],
60
        'acceptRejectSignature' => [
61
            Cancel\AcceptRejectSignatureService::class,
62
            Cancel\AcceptRejectSignatureCommand::class,
63
        ],
64
        'datetime' => [Utilities\DatetimeService::class, ''],
65
        'datetimePostalCode' => [Utilities\DatetimeService::class, Utilities\DatetimeCommand::class, 'datetime'],
66
        'downloadXml' => [Utilities\DownloadXmlService::class, Utilities\DownloadXmlCommand::class],
67
        'reportCredit' => [Utilities\ReportCreditService::class, Utilities\ReportCreditCommand::class],
68
        'reportTotal' => [Utilities\ReportTotalService::class, Utilities\ReportTotalCommand::class],
69
        'reportUuid' => [Utilities\ReportUuidService::class, Utilities\ReportUuidCommand::class],
70
        'getContracts' => [Manifest\GetContractsService::class, Manifest\GetContractsCommand::class, 'obtainContracts'],
71
        'signContracts' => [
72
            Manifest\SignContractsService::class,
73
            Manifest\SignContractsCommand::class,
74
            'sendSignedContracts',
75
        ],
76
    ];
77
78
    /** @var FinkokSettings */
79
    private $settings;
80
81 8
    public function __construct(FinkokSettings $factory)
82
    {
83 8
        $this->settings = $factory;
84 8
    }
85
86 1
    public function settings(): FinkokSettings
87
    {
88 1
        return $this->settings;
89
    }
90
91 5
    public function __call($name, $arguments)
92
    {
93 5
        if (array_key_exists($name, static::SERVICES_MAP)) {
94 4
            $command = $this->checkCommand($name, $arguments[0] ?? null);
95 3
            $service = $this->createService($name);
96 3
            $result = $this->executeService($name, $service, $command);
97 3
            return $result;
98
        }
99 1
        throw new BadMethodCallException(sprintf('Helper %s is not registered', $name));
100
    }
101
102 4
    protected function checkCommand(string $method, $command)
103
    {
104 4
        $expected = static::SERVICES_MAP[$method][1];
105 4
        if ('' === $expected) {
106 1
            return null;
107
        }
108 3
        if (! is_a($command, $expected)) {
109 1
            $type = (is_object($command)) ? get_class($command) : gettype($command);
110 1
            throw new InvalidArgumentException(
111 1
                sprintf('Call %s::%s expect %s but received %s', static::class, $method, $expected, $type)
112
            );
113
        }
114 2
        return $command;
115
    }
116
117 3
    protected function createService(string $method)
118
    {
119 3
        $serviceClass = static::SERVICES_MAP[$method][0];
120 3
        $service = new $serviceClass($this->settings);
121 3
        return $service;
122
    }
123
124 2
    protected function executeService(string $method, $service, $command)
125
    {
126 2
        $method = static::SERVICES_MAP[$method][2] ?? $method;
127 2
        if (! is_callable([$service, $method])) {
128 1
            throw new BadMethodCallException(
129 1
                sprintf('The service %s does not have a method %s', get_class($service), $method)
130
            );
131
        }
132 1
        return $service->{$method}($command);
133
    }
134
}
135