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

AcceptRejectUuidList   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 37
ccs 23
cts 23
cp 1
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findByUuid() 0 8 3
A findByUuidOrFail() 0 7 2
A createItemFromStdClass() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\Finkok\Services\Cancel;
6
7
use ArrayIterator;
8
use OutOfRangeException;
9
use PhpCfdi\Finkok\Definitions\CancelAnswer;
10
use PhpCfdi\Finkok\Services\AbstractCollection;
11
use stdClass;
12
13
/**
14
 * @method AcceptRejectUuidItem get(int $index)
15
 * @method AcceptRejectUuidItem first()
16
 * @method ArrayIterator|AcceptRejectUuidItem[] getIterator()
17
 */
18
class AcceptRejectUuidList extends AbstractCollection
19
{
20 3
    public function findByUuidOrFail(string $uuid): AcceptRejectUuidItem
21
    {
22 3
        $found = $this->findByUuid($uuid);
23 3
        if (null === $found) {
24 1
            throw new OutOfRangeException(sprintf('UUID %s not found on result', $uuid));
25
        }
26 2
        return $found;
27
    }
28
29 5
    public function findByUuid(string $uuid): ?AcceptRejectUuidItem
30
    {
31 5
        foreach ($this->getIterator() as $item) {
32 5
            if (0 === strcasecmp($item->uuid(), $uuid)) {
33 5
                return $item;
34
            }
35
        }
36 2
        return null;
37
    }
38
39 9
    protected function createItemFromStdClass(stdClass $content): object
40
    {
41 9
        if (isset($content->{'Acepta'})) {
42 9
            $source = $content->{'Acepta'};
43 9
            $answer = CancelAnswer::accept();
44 9
        } elseif (isset($content->{'Rechaza'})) {
45 9
            $source = $content->{'Rechaza'};
46 9
            $answer = CancelAnswer::reject();
47
        } else {
48 1
            $source = (object)[];
49 1
            $answer = CancelAnswer::accept();
50
        }
51 9
        return new AcceptRejectUuidItem(
52 9
            strval($source->uuid ?? ''),
53 9
            new AcceptRejectUuidStatus($source->status ?? '0'),
54 9
            $answer
55
        );
56
    }
57
}
58