Factory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.77%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 11
c 5
b 1
f 0
lcom 1
cbo 0
dl 0
loc 77
ccs 30
cts 31
cp 0.9677
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B registerElementType() 0 22 5
B createFromOptions() 0 26 6
1
<?php
2
namespace Sirius\Input\Element;
3
4
use Sirius\Input\Element\Input;
5
use Sirius\Input\Element;
6
use Sirius\Input\Specs;
7
8
class Factory
9
{
10
11
    protected $types = array(
12
        'text'        => '\Sirius\Input\Element\Input\Text',
13
        'file'        => '\Sirius\Input\Element\Input\File',
14
        'textarea'    => '\Sirius\Input\Element\Input\Textarea',
15
        'select'      => '\Sirius\Input\Element\Input\Select',
16
        'multiselect' => '\Sirius\Input\Element\Input\MultiSelect',
17
        'checkbox'    => '\Sirius\Input\Element\Input\Checkbox',
18
        'button'      => '\Sirius\Input\Element\Button',
19
        'submit'      => '\Sirius\Input\Element\Button\Submit',
20
        'reset'       => '\Sirius\Input\Element\Button\Reset',
21
        'group'       => '\Sirius\Input\Element\Group',
22
        'collection'  => '\Sirius\Input\Element\Collection',
23
        'fieldset'    => '\Sirius\Input\Element\Fieldset',
24
    );
25
26 5
    public function registerElementType($type, $classOrClosure)
27
    {
28 5
        if ($classOrClosure instanceof \Closure) {
29 1
            $this->types[$type] = $classOrClosure;
30
31 1
            return $this;
32
        }
33 4
        if (!is_string($classOrClosure)) {
34 1
            throw new \RuntimeException('Input type must be a class or a closure');
35
        }
36 4
        if (!class_exists($classOrClosure)) {
37 1
            throw new \RuntimeException(sprintf('Class %s does not exist', $classOrClosure));
38
        }
39 2
        if (!is_subclass_of($classOrClosure, '\Sirius\Input\Element')) {
40 1
            throw new \RuntimeException(
41 1
                sprintf('Class %s must extend the \Sirius\Input\Element class', $classOrClosure)
42 1
            );
43
        }
44 1
        $this->types[$type] = $classOrClosure;
45
46 1
        return $this;
47
    }
48
49
    /**
50
     * Create element from options
51
     *
52
     * @param $name
53
     * @param array $options
54
     *
55
     * @return \Sirius\Input\Element
56
     * @throws \RuntimeException
57
     */
58 19
    public function createFromOptions($name, $options = array())
59
    {
60 19
        $type = 'text';
61 19
        if (isset($options[Specs::TYPE]) && isset($this->types[$options[Specs::TYPE]])) {
62 14
            $type = $options[Specs::TYPE];
63 14
            unset($options[Specs::TYPE]);
64 14
        }
65
        /* @var $element \Sirius\Input\Element */
66 19
        if ($this->types[$type] instanceof \Closure) {
67 1
            $element = call_user_func($this->types[$type], $name, $options);
68 1
        } else {
69 18
            $class   = $this->types[$type];
70 18
            $element = new $class($name, $options);
71
        }
72
73 19
        if (!$element instanceof Element) {
74
            throw new \RuntimeException('Cannot create a valid element based on the data provided');
75
        }
76
77
        // if the element is a fieldset/collection type, inject the element factory
78 19
        if ($element instanceof FactoryAwareInterface) {
79 5
            $element->setElementFactory($this);
80 5
        }
81
82 19
        return $element;
83
    }
84
}
85