Passed
Push — master ( a8e753...d3e13e )
by Carlos C
05:06 queued 03:16
created

GetSatStatusService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 39
ccs 22
cts 22
cp 1
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 __construct() 0 3 1
A queryUntilFoundOrTime() 0 12 4
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 4
    }
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
        ]);
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