Passed
Push — v1 ( d8ddb5...d163cc )
by Andrew
12:26 queued 04:57
created

PluginTemplate::renderStringTemplate()   A

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 3.x
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 nystudio107\minify\Minify;
0 ignored issues
show
Bug introduced by
The type nystudio107\minify\Minify was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
use Craft;
16
use craft\helpers\Template;
17
use craft\web\View;
18
19
use yii\base\Exception;
20
21
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
22
 * @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...
23
 * @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...
24
 * @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...
25
 */
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...
26
class PluginTemplate
27
{
28
    // Constants
29
    // =========================================================================
30
31
    const MINIFY_PLUGIN_HANDLE = 'minify';
32
33
    // Static Methods
34
    // =========================================================================
35
36
    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...
37
    {
38
        try {
39
            $html = Craft::$app->getView()->renderString($templateString, $params);
40
        } catch (\Exception $e) {
41
            $html = Craft::t(
42
                'image-optimize',
43
                'Error rendering template string -> {error}',
44
                ['error' => $e->getMessage()]
45
            );
46
            Craft::error($html, __METHOD__);
47
        }
48
49
        return $html;
50
    }
51
52
    /**
53
     * Render a plugin template
54
     *
55
     * @param string      $templatePath
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
56
     * @param array       $params
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
57
     * @param string|null $minifier
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
58
     *
59
     * @return string
60
     */
61
    public static function renderPluginTemplate(
62
        string $templatePath,
63
        array $params = [],
64
        string $minifier = null
65
    ): string {
66
        $template = 'image-optimize/' . $templatePath;
67
        $oldMode = Craft::$app->view->getTemplateMode();
68
        // Look for the template on the frontend first
69
        try {
70
            $templateMode = View::TEMPLATE_MODE_CP;
71
            if (Craft::$app->view->doesTemplateExist($template, View::TEMPLATE_MODE_SITE)) {
72
                $templateMode = View::TEMPLATE_MODE_SITE;
73
            }
74
            Craft::$app->view->setTemplateMode($templateMode);
75
        } catch (Exception $e) {
76
            Craft::error($e->getMessage(), __METHOD__);
77
        }
78
79
        // Render the template with our vars passed in
80
        try {
81
            $htmlText = Craft::$app->view->renderTemplate($template, $params);
82
            if ($minifier) {
83
                // If Minify is installed, use it to minify the template
84
                $minify = Craft::$app->getPlugins()->getPlugin(self::MINIFY_PLUGIN_HANDLE);
85
                if ($minify) {
86
                    $htmlText = Minify::$plugin->minify->$minifier($htmlText);
87
                }
88
89
            }
90
        } catch (\Exception $e) {
91
            $htmlText = Craft::t(
92
                'image-optimize',
93
                'Error rendering `{template}` -> {error}',
94
                ['template' => $templatePath, 'error' => $e->getMessage()]
95
            );
96
            Craft::error($htmlText, __METHOD__);
97
        }
98
99
        // Restore the old template mode
100
        try {
101
            Craft::$app->view->setTemplateMode($oldMode);
102
        } catch (Exception $e) {
103
            Craft::error($e->getMessage(), __METHOD__);
104
        }
105
106
        return Template::raw($htmlText);
107
    }
108
}
109