Passed
Pull Request — master (#11)
by Carlos C
01:51
created

GetSatStatusService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 59.09%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 39
ccs 13
cts 22
cp 0.5909
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A settings() 0 3 1
A query() 0 10 1
A queryUntilFoundOrTime() 0 12 4
A __construct() 0 3 1
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