DefinitionCollection::validateFactory()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
dl 6
loc 6
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Isolate\Framework\UnitOfWork\Entity;
4
5
use Isolate\Framework\UnitOfWork\Entity\Definition\Factory;
6
use Isolate\UnitOfWork\Entity\Definition;
7
8 View Code Duplication
class DefinitionCollection implements \IteratorAggregate 
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
9
{
10
    private $definitions;
11
12
    /**
13
     * @param array $elements
14
     */
15
    public function __construct(array $elements = array())
16
    {
17
        $this->definitions = [];
18
        
19
        foreach ($elements as $definitionFactory) {
20
            $this->validateFactory($definitionFactory);
21
            $definition = $definitionFactory->createDefinition();
22
            $this->validateDefinition($definition);
23
            
24
            $this->definitions[] = $definition;
25
        }
26
    }
27
28
    /**
29
     * @param Factory $definitionFactory
30
     * @return bool
31
     */
32
    public function add(Factory $definitionFactory)
33
    {
34
        $definition = $definitionFactory->createDefinition();
35
        $this->validateDefinition($definition);
36
        
37
        $this->definitions[] = $definition;
38
    }
39
40
    public function getIterator()
41
    {
42
        return new \ArrayIterator($this->definitions);
43
    }
44
45
    /**
46
     * @param $definitionFactory
47
     */
48
    private function validateFactory($definitionFactory)
49
    {
50
        if (!$definitionFactory instanceof Factory) {
51
            throw new \InvalidArgumentException("Definition collection elements needs to implement Definition\\Factory.");
52
        }
53
    }
54
55
    /**
56
     * @param $definition
57
     */
58
    private function validateDefinition($definition)
59
    {
60
        if (!$definition instanceof Definition) {
61
            throw new \RuntimeException("Definition\\Factory needs to create Entity\\Definition instance.");
62
        }
63
    }
64
}
65