Completed
Push — feature/improve-form-definitio... ( 0cba0f...350a13 )
by Romain
02:18
created

RootConfigurationJavaScriptAssetHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getJavaScriptFileName() 0 10 1
A getJavaScriptCode() 0 10 1
A handleRootConfiguration() 0 4 1
A getRootConfiguration() 0 13 1
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\AssetHandler\JavaScript;
15
16
use Romm\Formz\AssetHandler\AbstractAssetHandler;
17
use Romm\Formz\Service\ArrayService;
18
use Romm\Formz\Service\CacheService;
19
20
/**
21
 * This asset handler generates the JavaScript code which will inject the FormZ
22
 * TypoScript configuration.
23
 */
24
class RootConfigurationJavaScriptAssetHandler extends AbstractAssetHandler
25
{
26
27
    /**
28
     * @return string
29
     */
30
    public function getJavaScriptFileName()
31
    {
32
        $hash = sha1($this->getFormObject()
33
            ->getDefinition()
34
            ->getRootConfiguration()
35
            ->getHash()
36
        );
37
38
        return CacheService::GENERATED_FILES_PATH . 'fz-config-' . $hash . '.js';
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getJavaScriptCode()
45
    {
46
        $jsonRootConfiguration = $this->handleRootConfiguration($this->getRootConfiguration());
47
48
        return <<<JS
49
(function() {
50
    Fz.setConfiguration($jsonRootConfiguration);
51
})();
52
JS;
53
    }
54
55
    /**
56
     * This function is here to help unit tests mocking.
57
     *
58
     * @param string $rootConfiguration
59
     * @return string
60
     */
61
    protected function handleRootConfiguration($rootConfiguration)
62
    {
63
        return $rootConfiguration;
64
    }
65
66
    /**
67
     * Returns a JSON array containing the root configuration.
68
     *
69
     * @return string
70
     */
71
    protected function getRootConfiguration()
72
    {
73
        $rootConfigurationArray = $this->getFormObject()
74
            ->getDefinition()
75
            ->getRootConfiguration()
76
            ->toArray();
77
78
        $cleanRootConfigurationArray = [
79
            'view' => $rootConfigurationArray['view']
80
        ];
81
82
        return ArrayService::get()->arrayToJavaScriptJson($cleanRootConfigurationArray);
83
    }
84
}
85