Passed
Push — master ( f78dc8...d3e9de )
by Carlos C
46s queued 12s
created

GetSatStatusService::queryUntilFoundOrTime()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 2
nop 2
dl 0
loc 12
ccs 0
cts 9
cp 0
crap 20
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 2
    public function __construct(FinkokSettings $settings)
16
    {
17 2
        $this->settings = $settings;
18 2
    }
19
20 1
    public function settings(): FinkokSettings
21
    {
22 1
        return $this->settings;
23
    }
24
25 1
    public function query(GetSatStatusCommand $command): GetSatStatusResult
26
    {
27 1
        $soapCaller = $this->settings()->createCallerForService(Services::cancel());
28 1
        $rawResponse = $soapCaller->call('get_sat_status', [
29 1
            'taxpayer_id' => $command->rfcIssuer(),
30 1
            'rtaxpayer_id' => $command->rfcRecipient(),
31 1
            'uuid' => $command->uuid(),
32 1
            'total' => $command->total(),
33
        ]);
34 1
        return new GetSatStatusResult($rawResponse);
35
    }
36
37
    public function queryUntilFoundOrTime(GetSatStatusCommand $command, int $waitSeconds = 120): GetSatStatusResult
38
    {
39
        $runUntilTime = time() + $waitSeconds;
40
        do {
41
            $result = $this->query($command);
42
            if ('No Encontrado' === $result->cfdi() && time() <= $runUntilTime) {
43
                usleep(200000);
44
                continue;
45
            }
46
            break;
47
        } while (true);
48
        return $result;
49
    }
50
}
51