CssAssetHandlerConnector::includeGeneratedCss()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Connector;
15
16
use Romm\Formz\AssetHandler\Css\FieldsActivationCssAssetHandler;
17
use Romm\Formz\AssetHandler\Css\MessageContainerDisplayCssAssetHandler;
18
use Romm\Formz\Service\StringService;
19
20
class CssAssetHandlerConnector
21
{
22
    /**
23
     * List of CSS files which will be included whenever this view helper is
24
     * used.
25
     *
26
     * @var array
27
     */
28
    private $cssFiles = [
29
        'Form.Main.css'
30
    ];
31
32
    /**
33
     * @var AssetHandlerConnectorManager
34
     */
35
    private $assetHandlerConnectorManager;
36
37
    /**
38
     * @param AssetHandlerConnectorManager $assetHandlerConnectorManager
39
     */
40
    public function __construct(AssetHandlerConnectorManager $assetHandlerConnectorManager)
41
    {
42
        $this->assetHandlerConnectorManager = $assetHandlerConnectorManager;
43
    }
44
45
    /**
46
     * Will include all default CSS files declared in the property `$cssFiles`
47
     * of this class.
48
     *
49
     * @return $this
50
     */
51
    public function includeDefaultCssFiles()
52
    {
53
        foreach ($this->cssFiles as $file) {
54
            $filePath = StringService::get()->getExtensionRelativePath('Resources/Public/StyleSheets/' . $file);
55
56
            $this->assetHandlerConnectorManager
57
                ->getPageRenderer()
58
                ->addCssFile($filePath);
59
        }
60
61
        return $this;
62
    }
63
64
    /**
65
     * Will take care of generating the CSS with the `AssetHandlerFactory`. The
66
     * code will be put in a `.css` file in the `typo3temp` directory.
67
     *
68
     * If the file already exists, it is included directly before the code
69
     * generation.
70
     *
71
     * @return $this
72
     */
73
    public function includeGeneratedCss()
74
    {
75
        $filePath = $this->assetHandlerConnectorManager->getFormzGeneratedFilePath() . '.css';
76
77
        $this->assetHandlerConnectorManager->createFileInTemporaryDirectory(
78
            $filePath,
79
            function () {
80
                /** @var MessageContainerDisplayCssAssetHandler $errorContainerDisplayCssAssetHandler */
81
                $errorContainerDisplayCssAssetHandler = $this->assetHandlerConnectorManager
82
                    ->getAssetHandlerFactory()
83
                    ->getAssetHandler(MessageContainerDisplayCssAssetHandler::class);
84
85
                /** @var FieldsActivationCssAssetHandler $fieldsActivationCssAssetHandler */
86
                $fieldsActivationCssAssetHandler = $this->assetHandlerConnectorManager
87
                    ->getAssetHandlerFactory()
88
                    ->getAssetHandler(FieldsActivationCssAssetHandler::class);
89
90
                $css = $errorContainerDisplayCssAssetHandler->getErrorContainerDisplayCss() . LF;
91
                $css .= $fieldsActivationCssAssetHandler->getFieldsActivationCss();
92
93
                return $css;
94
            }
95
        );
96
97
        $this->assetHandlerConnectorManager
98
            ->getPageRenderer()
99
            ->addCssFile(StringService::get()->getResourceRelativePath($filePath));
100
101
        return $this;
102
    }
103
}
104