AcceptRejectUuidList::findByUuidOrFail()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
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
 * @extends AbstractCollection<AcceptRejectUuidItem>
18
 */
19
class AcceptRejectUuidList extends AbstractCollection
20
{
21 3
    public function findByUuidOrFail(string $uuid): AcceptRejectUuidItem
22
    {
23 3
        $found = $this->findByUuid($uuid);
24 3
        if (null === $found) {
25 1
            throw new OutOfRangeException(sprintf('UUID %s not found on result', $uuid));
26
        }
27 2
        return $found;
28
    }
29
30 5
    public function findByUuid(string $uuid): ?AcceptRejectUuidItem
31
    {
32 5
        foreach ($this->getIterator() as $item) {
33 5
            if (0 === strcasecmp($item->uuid(), $uuid)) {
34 3
                return $item;
35
            }
36
        }
37 2
        return null;
38
    }
39
40 9
    protected function createItemFromStdClass(stdClass $content): object
41
    {
42 9
        if (isset($content->{'Acepta'})) {
43 9
            $source = $content->{'Acepta'};
44 9
            $answer = CancelAnswer::accept();
45 9
        } elseif (isset($content->{'Rechaza'})) {
46 9
            $source = $content->{'Rechaza'};
47 9
            $answer = CancelAnswer::reject();
48
        } else {
49 1
            $source = (object)[];
50 1
            $answer = CancelAnswer::accept();
51
        }
52 9
        return new AcceptRejectUuidItem(
53 9
            strval($source->uuid ?? ''),
54 9
            new AcceptRejectUuidStatus($source->status ?? '0'),
55 9
            $answer
56 9
        );
57
    }
58
}
59