Factory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 0 Features 1
Metric Value
wmc 6
c 8
b 0
f 1
lcom 0
cbo 1
dl 0
loc 37
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A instantiate() 0 12 2
A get() 0 14 4
1
<?php
2
3
namespace Zerg\Field;
4
5
/**
6
 * Field factory class. Instantiates fields by their declarations.
7
 *
8
 * @since 0.1
9
 * @package Zerg\Field
10
 */
11
class Factory
12
{
13
    /**
14
     * Create field instance by its declaration.
15
     *
16
     * @param array $declaration Field declaration.
17
     * @return AbstractField Field instance.
18
     * @throws ConfigurationException If invalid declaration is presented.
19
     */
20 21
    private static function instantiate(array $declaration)
21
    {
22 21
        $fieldType = array_shift($declaration);
23
24 21
        $class = "\\Zerg\\Field\\" . ucfirst(strtolower($fieldType));
25 21
        if (class_exists($class)) {
26 20
            $reflection = new \ReflectionClass($class);
27 20
            return $reflection->newInstanceArgs($declaration);
28
        }
29
30 1
        throw new ConfigurationException("Field {$fieldType} doesn't exist");
31
    }
32
33 22
    public static function get($array)
34
    {
35 22
        if (!is_array($array)) {
36 1
            throw new ConfigurationException('Unknown element declaration');
37
        }
38
39 21
        $isAssoc = array_keys(array_keys($array)) !== array_keys($array);
40
41 21
        if ($isAssoc || is_array(reset($array))) {
42 4
            $array = ['collection', $array];
43 4
        }
44
45 21
        return Factory::instantiate($array);
46
    }
47
}