Passed
Push — master ( fb46b6...9082b3 )
by Carlos C
49s queued 11s
created

Finkok::executeService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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