Passed
Pull Request — develop (#62)
by Kevin
02:57
created

MagiumRenderer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 44
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 31 4
A configureFormView() 0 8 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: kschr
5
 * Date: 3/20/2017
6
 * Time: 4:54 PM
7
 */
8
9
namespace Magium\Configuration\View;
10
11
use Zend\Form\Element;
12
use Zend\Form\ElementInterface;
13
use Zend\Form\View\Helper\FormElement;
14
use Zend\Form\View\Helper\FormInput;
15
use Zend\Form\View\Helper\FormSelect;
16
use Zend\View\Helper\AbstractHelper;
17
use Zend\View\Renderer\RendererInterface;
18
19
class MagiumRenderer extends AbstractHelper
20
{
21 1
    public function __invoke($section, $group, $id, $options)
22
    {
23 1
        $type = 'text';
24 1
        if (isset($options['type'])) {
25 1
            $type = $options['type'];
26
        }
27 1
        $value = $options['value'];
28
29 1
        $viewClass = 'Zend\Form\View\Helper\Form' . ucfirst(strtolower($type));
30 1
        $formClass = 'Zend\Form\Element\\' . ucfirst(strtolower($type));
31 1
        $reflectionClass = new \ReflectionClass($viewClass);
32 1
        if (!$reflectionClass->isSubclassOf(AbstractHelper::class)) {
33
            throw new InvalidViewConfigurationException('Invalid setting input type');
34
        }
35
36 1
        $instance = $reflectionClass->newInstance();
37 1
        $formInstance = new \ReflectionClass($formClass);
38 1
        $this->configureFormView($instance);
39 1
        $name = sprintf('%s_%s_%s', $section, $group, $id);
40 1
        $formElement = $formInstance->newInstance($name, $options);
41
        /* @var $formElement Element */
42 1
        if ($formElement instanceof Element\Select) {
43
            $formElement->setOptions(['options' => $options['source']]);
44
        }
45 1
        $formElement->setAttribute('onchange', 'magiumRegisterChange(event)');
46 1
        $formElement->setAttribute('class', 'form-control');
47 1
        $formElement->setAttribute('data-path', $options['path']);
48 1
        $formElement->setValue($value);
49 1
        $output = $instance->render($formElement);
50 1
        return $output;
51
    }
52
53 1
    protected function configureFormView(AbstractHelper $instance)
54
    {
55 1
        $view = $this->getView();
56 1
        if (!$view instanceof RendererInterface) {
57
            throw new InvalidViewConfigurationException('View is not the correct instance type');
58
        }
59 1
        $instance->setView($view);
60 1
    }
61
62
}
63