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

AcceptRejectUuidList::findByUuid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
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
 */
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