SimpleFormProvider::formatData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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