PluginTemplate::renderStringTemplate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 14
rs 9.9666
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * ImageOptimize plugin for Craft CMS
4
 *
5
 * Automatically optimize images after they've been transformed
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2021 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\imageoptimize\helpers;
12
13
use Craft;
14
use craft\helpers\Template;
15
use craft\web\View;
16
use nystudio107\minify\Minify;
17
use yii\base\Exception;
18
19
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
20
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
21
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
22
 * @since     1.7.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
23
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
24
class PluginTemplate
25
{
26
    // Constants
27
    // =========================================================================
28
29
    public const MINIFY_PLUGIN_HANDLE = 'minify';
30
31
    // Static Methods
32
    // =========================================================================
33
34
    public static function renderStringTemplate(string $templateString, array $params = []): string
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
35
    {
36
        try {
37
            $html = Craft::$app->getView()->renderString($templateString, $params);
38
        } catch (\Exception $e) {
39
            $html = Craft::t(
40
                'image-optimize',
41
                'Error rendering template string -> {error}',
42
                ['error' => $e->getMessage()]
43
            );
44
            Craft::error($html, __METHOD__);
45
        }
46
47
        return $html;
48
    }
49
50
    /**
51
     * Render a plugin template
52
     *
53
     * @param string $templatePath
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
54
     * @param array $params
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
55
     * @param string|null $minifier
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
56
     *
57
     * @return string
58
     */
59
    public static function renderPluginTemplate(
60
        string $templatePath,
61
        array  $params = [],
62
        string $minifier = null,
63
    ): string {
64
        $template = 'image-optimize/' . $templatePath;
65
        $oldMode = Craft::$app->view->getTemplateMode();
66
        // Look for the template on the frontend first
67
        try {
68
            $templateMode = View::TEMPLATE_MODE_CP;
69
            if (Craft::$app->view->doesTemplateExist($template, View::TEMPLATE_MODE_SITE)) {
70
                $templateMode = View::TEMPLATE_MODE_SITE;
71
            }
72
            Craft::$app->view->setTemplateMode($templateMode);
73
        } catch (Exception $e) {
74
            Craft::error($e->getMessage(), __METHOD__);
75
        }
76
77
        // Render the template with our vars passed in
78
        try {
79
            $htmlText = Craft::$app->view->renderTemplate($template, $params);
80
            if ($minifier) {
81
                // If Minify is installed, use it to minify the template
82
                $minify = Craft::$app->getPlugins()->getPlugin(self::MINIFY_PLUGIN_HANDLE);
83
                if ($minify) {
84
                    $htmlText = Minify::$plugin->minify->$minifier($htmlText);
85
                }
86
            }
87
        } catch (\Exception $e) {
88
            $htmlText = Craft::t(
89
                'image-optimize',
90
                'Error rendering `{template}` -> {error}',
91
                ['template' => $templatePath, 'error' => $e->getMessage()]
92
            );
93
            Craft::error($htmlText, __METHOD__);
94
        }
95
96
        // Restore the old template mode
97
        try {
98
            Craft::$app->view->setTemplateMode($oldMode);
99
        } catch (Exception $e) {
100
            Craft::error($e->getMessage(), __METHOD__);
101
        }
102
103
        return Template::raw($htmlText);
104
    }
105
}
106