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

AbstractCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 36
ccs 15
cts 15
cp 1
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 6 2
A first() 0 3 1
A __construct() 0 5 2
A getIterator() 0 3 1
A count() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\Finkok\Services;
6
7
use ArrayIterator;
8
use ArrayObject;
9
use Countable;
10
use IteratorAggregate;
11
use stdClass;
12
13
abstract class AbstractCollection implements Countable, IteratorAggregate
14
{
15
    abstract protected function createItemFromStdClass(stdClass $content): object;
16
17
    /** @var ArrayObject */
18
    protected $collection;
19
20 36
    public function __construct(array $collection)
21
    {
22 36
        $this->collection = new ArrayObject();
23 36
        foreach ($collection as $content) {
24 31
            $this->collection->append($this->createItemFromStdClass($content));
25
        }
26 36
    }
27
28 11
    public function get(int $index): object
29
    {
30 11
        if (! isset($this->collection[$index])) {
31 2
            return $this->createItemFromStdClass((object) []);
32
        }
33 9
        return $this->collection[$index];
34
    }
35
36 18
    public function count(): int
37
    {
38 18
        return $this->collection->count();
39
    }
40
41 5
    public function first(): object
42
    {
43 5
        return $this->get(0);
44
    }
45
46 8
    public function getIterator(): ArrayIterator
47
    {
48 8
        return $this->collection->getIterator();
49
    }
50
}
51