GetSatStatusService::queryUntilFoundOrTime()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 2
nop 2
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 4
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\Finkok\Services\Cancel;
6
7
use PhpCfdi\Finkok\Definitions\Services;
8
use PhpCfdi\Finkok\FinkokSettings;
9
10
class GetSatStatusService
11
{
12
    /** @var FinkokSettings */
13
    private $settings;
14
15 4
    public function __construct(FinkokSettings $settings)
16
    {
17 4
        $this->settings = $settings;
18
    }
19
20 3
    public function settings(): FinkokSettings
21
    {
22 3
        return $this->settings;
23
    }
24
25 3
    public function query(GetSatStatusCommand $command): GetSatStatusResult
26
    {
27 3
        $soapCaller = $this->settings()->createCallerForService(Services::cancel());
28 3
        $rawResponse = $soapCaller->call('get_sat_status', [
29 3
            'taxpayer_id' => $command->rfcIssuer(),
30 3
            'rtaxpayer_id' => $command->rfcRecipient(),
31 3
            'uuid' => $command->uuid(),
32 3
            'total' => $command->total(),
33 3
        ]);
34 3
        return new GetSatStatusResult($rawResponse);
35
    }
36
37 1
    public function queryUntilFoundOrTime(GetSatStatusCommand $command, int $waitSeconds = 120): GetSatStatusResult
38
    {
39 1
        $runUntilTime = time() + $waitSeconds;
40
        do {
41 1
            $result = $this->query($command);
42 1
            if ('No Encontrado' === $result->cfdi() && time() <= $runUntilTime) {
43 1
                usleep(200000);
44 1
                continue;
45
            }
46 1
            break;
47 1
        } while (true);
48 1
        return $result;
49
    }
50
}
51