EntityFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 62
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 16 4
A createCollection() 0 11 2
1
<?php
2
3
namespace T4webDomain;
4
5
use T4webDomainInterface\EntityFactoryInterface;
6
use T4webDomainInterface\EntityInterface;
7
8
class EntityFactory implements EntityFactoryInterface
9
{
10
11
    /**
12
     * @var string
13
     */
14
    protected $entityClass;
15
16
    /**
17
     * @var string
18
     */
19
    protected $collectionClass;
20
21
    /**
22
     * @param string $entityClass
23
     * @param string $collectionClass
24
     */
25
    public function __construct($entityClass, $collectionClass = Collection::class)
26
    {
27
        $this->entityClass = $entityClass;
28
        $this->collectionClass = $collectionClass;
29
    }
30
31
    /**
32
     * @param array $data
33
     *
34
     * @return EntityInterface
35
     */
36
    public function create(array $data)
37
    {
38
        if (!isset($data['data']) && !isset($data['aggregateItems'])) {
39
            return new $this->entityClass($data);
40
        }
41
42
        $reflector = new \ReflectionClass($this->entityClass);
43
44
        $istanceArgs = [$data['data']];
45
46
        foreach ($data['aggregateItems'] as $aggregateItem) {
47
            $istanceArgs[] = $aggregateItem;
48
        }
49
50
        return $reflector->newInstanceArgs($istanceArgs);
51
    }
52
53
    /**
54
     * @param array $data
55
     *
56
     * @return ArrayObject
57
     */
58
    public function createCollection(array $data)
59
    {
60
        $collection = new $this->collectionClass();
61
62
        foreach ($data as $value) {
63
            $entity = $this->create($value);
64
            $collection->offsetSet($entity->getId(), $entity);
65
        }
66
67
        return $collection;
68
    }
69
}
70