Completed
Push — master ( 3e70d8...0fa5e9 )
by
unknown
03:54
created

FormzConfigurationJavaScriptAssetHandler::getFormzConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
/*
3
 * 2016 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\Core\Core;
17
18
/**
19
 * This asset handler generates the JavaScript code which will inject the Formz
20
 * TypoScript configuration.
21
 */
22
class FormzConfigurationJavaScriptAssetHandler extends AbstractJavaScriptAssetHandler
23
{
24
25
    /**
26
     * @return string
27
     */
28
    public function getJavaScriptFileName()
29
    {
30
        $hash = $this->getFormObject()
31
            ->getConfiguration()
32
            ->getFormzConfiguration()
33
            ->getHash();
34
35
        return Core::GENERATED_FILES_PATH . 'formz-config-' . $hash . '.js';
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getJavaScriptCode()
42
    {
43
        $jsonFormzConfiguration = $this->handleFormzConfiguration($this->getFormzConfiguration());
44
45
        return <<<JS
46
(function() {
47
    Formz.setConfiguration($jsonFormzConfiguration);
48
})();
49
JS;
50
    }
51
52
    /**
53
     * This function is here to help unit tests mocking.
54
     *
55
     * @param string $formzConfiguration
56
     * @return string
57
     */
58
    protected function handleFormzConfiguration($formzConfiguration)
59
    {
60
        return $formzConfiguration;
61
    }
62
63
    /**
64
     * Returns a JSON array containing the formz configuration.
65
     *
66
     * @return string
67
     */
68
    protected function getFormzConfiguration()
69
    {
70
        $formzConfigurationArray = $this->getFormObject()
71
            ->getConfiguration()
72
            ->getFormzConfiguration()
73
            ->toArray();
74
75
        $cleanFormzConfigurationArray = [
76
            'view' => $formzConfigurationArray['view']
77
        ];
78
79
        return Core::get()->arrayToJavaScriptJson($cleanFormzConfigurationArray);
80
    }
81
}
82