Completed
Push — master ( 2df7ac...02da02 )
by De Cramer
19s queued 11s
created

SimpleFormProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
eloc 22
c 1
b 0
f 1
dl 0
loc 77
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addTypeToForm() 0 9 1
A getHelpHtml() 0 14 3
A formatData() 0 4 1
A supports() 0 3 1
1
<?php
2
3
namespace oliverde8\ComfyBundle\Resolver\Form;
4
5
6
use oliverde8\ComfyBundle\Model\ConfigInterface;
7
use oliverde8\ComfyBundle\Resolver\FormTypeProviderInterface;
8
use oliverde8\ComfyBundle\Resolver\ScopeResolverInterface;
9
use Symfony\Component\Form\FormInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Form\FormInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
class SimpleFormProvider implements FormTypeProviderInterface
12
{
13
    protected $scopeResolver;
14
15
    /** @var string */
16
    protected string $configType;
17
18
    /** @var string */
19
    protected string $formType;
20
21
    /**
22
     * SimpleFormProvider constructor.
23
     *
24
     * @param string $configType
25
     * @param string $formType
26
     */
27
    public function __construct(ScopeResolverInterface $scopeResolver, string $configType, string $formType)
28
    {
29
        $this->scopeResolver = $scopeResolver;
30
        $this->configType = $configType;
31
        $this->formType = $formType;
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function addTypeToForm(string $name, ConfigInterface $config, FormInterface $formBuilder, string $scope)
38
    {
39
        $formBuilder->add(
40
            $name,
41
            $this->formType,
42
            [
43
                'label' => $config->getName(),
44
                'help' => $this->getHelpHtml($config, $scope),
45
                'data' => $config->get($scope),
46
            ],
47
        );
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function formatData($data)
54
    {
55
        // No formatting needed.
56
        return $data;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function supports(ConfigInterface $config): bool
63
    {
64
        return $config instanceof $this->configType;
65
    }
66
67
    /**
68
     * Get html for the help field.
69
     *
70
     * @param ConfigInterface $config
71
     * @param string|null $scope
72
     * @return string
73
     */
74
    protected function getHelpHtml(ConfigInterface $config, ?string $scope)
75
    {
76
        $helpMessage = $config->getDescription();
77
78
        if (!$config->doesInherit($scope)) {
79
            $helpMessage .= "</br><strong>Default Value</strong>: " . $config->getDefaultValue();
80
81
            $parentScope = $this->scopeResolver->inherits($scope);
82
            if ($config->get($parentScope) != $config->getDefaultValue()) {
83
                $helpMessage .= "</br><strong>Parent Scope Value</strong>: " . json_encode($config->get($parentScope));
84
            }
85
        }
86
87
        return $helpMessage;
88
    }
89
}
90