Customers::createItemFromStdClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\Finkok\Services\Registration;
6
7
use ArrayIterator;
8
use LogicException;
9
use PhpCfdi\Finkok\Services\AbstractCollection;
10
use stdClass;
11
12
/**
13
 * @method Customer get(int $index)
14
 * @method Customer first()
15
 * @method ArrayIterator|Customer[] getIterator()
16
 * @extends AbstractCollection<Customer>
17
 */
18
class Customers extends AbstractCollection
19
{
20 19
    protected function createItemFromStdClass(stdClass $content): object
21
    {
22 19
        return new Customer($content);
23
    }
24
25 12
    public function getByRfc(string $rfc): Customer
26
    {
27 12
        $customer = $this->findByRfc($rfc);
28 12
        if (null === $customer) {
29 1
            throw new LogicException(sprintf('There is no customer with RFC %s', $rfc));
30
        }
31 11
        return $customer;
32
    }
33
34 18
    public function findByRfc(string $rfc): ?Customer
35
    {
36 18
        foreach ($this->getIterator() as $customer) {
37 14
            if ($rfc === $customer->rfc()) {
38 13
                return $customer;
39
            }
40
        }
41 8
        return null;
42
    }
43
44 2
    public function merge(self $customers): self
45
    {
46 2
        $clone = clone $this;
47 2
        foreach ($customers as $customer) {
48 2
            $clone->collection->append($customer);
49
        }
50 2
        return $clone;
51
    }
52
}
53