Completed
Push — master ( 28dda4...aabe55 )
by Craig
06:13
created

FormAwareHook::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\Bundle\HookBundle\FormAwareHook;
13
14
use Symfony\Component\Form\Exception\InvalidConfigurationException;
15
use Symfony\Component\Form\FormInterface;
16
use Zikula\Bundle\HookBundle\Hook\Hook;
17
18
class FormAwareHook extends Hook
19
{
20
    /**
21
     * @var FormInterface
22
     */
23
    private $form;
24
25
    /**
26
     * @var array
27
     */
28
    private $templates = [];
29
30
    /**
31
     * @param FormInterface $form
32
     */
33
    public function __construct(FormInterface $form)
34
    {
35
        $this->form = $form;
36
    }
37
38
    /**
39
     * @return mixed
40
     */
41
    public function getFormData()
42
    {
43
        return $this->form->getData();
44
    }
45
46
    /**
47
     * @param FormInterface|string|int $child
48
     * @param string|null $type
49
     * @param array $options
50
     * @return self
51
     */
52
    public function formAdd($child, $type = null, array $options = [])
53
    {
54
        if (($child instanceof FormInterface) && ($child->getConfig()->getMapped() || $child->getConfig()->getAutoInitialize())) {
1 ignored issue
show
Bug introduced by
The class Symfony\Component\Form\FormInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
55
            throw new InvalidConfigurationException('Hooked child forms must disable `mapped` and `auto_initialize` options.');
56
        } else {
57
            $options['mapped'] = false;
58
            $options['auto_initialize'] = false;
59
        }
60
        $this->form->add($child, $type, $options);
61
62
        return $this;
63
    }
64
65
    /**
66
     * @param string $template
67
     * @param array $templateVars
68
     * @return self
69
     */
70 View Code Duplication
    public function addTemplate($template, $templateVars = [])
0 ignored issues
show
Duplication introduced by
This method 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...
71
    {
72
        if (!in_array($template, $this->templates)) {
73
            $this->templates[] = [$template, $templateVars];
74
        }
75
76
        return $this;
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function getTemplates()
83
    {
84
        return $this->templates;
85
    }
86
}
87