Completed
Push — master ( c13142...c0435c )
by Tobias
02:34
created

CollectionItem   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 37
ccs 12
cts 14
cp 0.8571
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A createFromArray() 0 11 3
A getItems() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Billogram\Model\Item;
6
7
use Billogram\Exception\InvalidArgumentException;
8
use Billogram\Model\CreatableFromArray;
9
10
/**
11
 * @author Ibrahim Hizeoui <[email protected]>
12
 */
13
class CollectionItem implements CreatableFromArray
14
{
15
    /**
16
     * @var Item[]
17
     */
18
    private $items;
19
20 1
    private function __construct(array $items)
21
    {
22 1
        foreach ($items as $item) {
23 1
            if (!$item instanceof Item) {
24 1
                throw new InvalidArgumentException('All items must be an instance of '.Item::class);
25
            }
26
        }
27 1
        $this->items = $items;
28 1
    }
29
30 1
    public static function createFromArray(array $data)
31
    {
32 1
        $items = [];
33 1
        if (isset($data['data'])) {
34 1
            foreach ($data['data'] as $item) {
35 1
                $items[] = Item::createFromArray($item);
36
            }
37
        }
38
39 1
        return new self($items);
40
    }
41
42
    /**
43
     * @return Items[]
44
     */
45
    public function getItems()
46
    {
47
        return $this->items;
48
    }
49
}
50