Passed
Push — develop ( 8d02f5...9d045c )
by Andrew
12:56
created

PluginTemplate::renderPluginTemplate()   B

Complexity

Conditions 7
Paths 60

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 26
nc 60
nop 3
dl 0
loc 46
ccs 0
cts 38
cp 0
crap 56
rs 8.5706
c 0
b 0
f 0
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\helpers;
13
14
use nystudio107\seomatic\Seomatic;
15
16
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...
17
18
use Craft;
19
use craft\helpers\Template;
20
use craft\web\View;
21
22
use yii\base\Exception;
23
24
/**
25
 * @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...
26
 * @package   Seomatic
27
 * @since     3.0.0
28
 */
29
class PluginTemplate
30
{
31
    // Constants
32
    // =========================================================================
33
34
    const MINIFY_PLUGIN_HANDLE = 'minify';
35
36
    // Static Methods
37
    // =========================================================================
38
39
    /**
40
     * Render the passed in Twig string template, catching any errors
41
     *
42
     * @param string $templateString
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
43
     * @param array $params
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
44
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
45
     */
46
    public static function renderStringTemplate(string $templateString, array $params = []): string
47
    {
48
        try {
49
            $html = Seomatic::$view->renderString($templateString, $params);
50
        } catch (\Exception $e) {
51
            $html = Craft::t(
52
                'seomatic',
53
                'Error rendering template string -> {error}',
54
                ['error' => $e->getMessage()]
55
            );
56
            Craft::error($html, __METHOD__);
57
        }
58
59
        return $html;
60
    }
61
62
    /**
63
     * Return an error if the Twig template is invalid, or an empty string if no errors
64
     *
65
     * @param string $templateString
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
66
     * @param array $params
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
67
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
68
     */
69
    public static function isStringTemplateValid(string $templateString, array $params = []): string
70
    {
71
        $error = '';
72
        try {
73
            $html = Seomatic::$view->renderString($templateString, $params);
0 ignored issues
show
Unused Code introduced by
The assignment to $html is dead and can be removed.
Loading history...
74
        } catch (\Exception $e) {
75
            $error = Craft::t(
76
                'seomatic',
77
                'Error rendering template string -> {error}',
78
                ['error' => $e->getMessage()]
79
            );
80
            Craft::error($error, __METHOD__);
81
        }
82
83
        return $error;
84
    }
85
86
    /**
87
     * Render a plugin template
88
     *
89
     * @param string      $templatePath
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
90
     * @param array       $params
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
91
     * @param string|null $minifier
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
92
     *
93
     * @return string
94
     */
95
    public static function renderPluginTemplate(
96
        string $templatePath,
97
        array $params = [],
98
        string $minifier = null
99
    ): string {
100
        $template = 'seomatic/' . $templatePath;
101
        $oldMode = Craft::$app->view->getTemplateMode();
102
        // Look for the template on the frontend first
103
        try {
104
            $templateMode = View::TEMPLATE_MODE_CP;
105
            if (Craft::$app->view->doesTemplateExist($template, View::TEMPLATE_MODE_SITE)) {
106
                $templateMode = View::TEMPLATE_MODE_SITE;
107
            }
108
            Craft::$app->view->setTemplateMode($templateMode);
109
        } catch (Exception $e) {
110
            Craft::error($e->getMessage(), __METHOD__);
111
        }
112
113
        // Render the template with our vars passed in
114
        try {
115
            $htmlText = Craft::$app->view->renderTemplate($template, $params);
116
            if ($minifier) {
117
                // If Minify is installed, use it to minify the template
118
                $minify = Craft::$app->getPlugins()->getPlugin(self::MINIFY_PLUGIN_HANDLE);
119
                if ($minify) {
120
                    $htmlText = Minify::$plugin->minify->$minifier($htmlText);
121
                }
122
123
            }
124
        } catch (\Exception $e) {
125
            $htmlText = Craft::t(
126
                'seomatic',
127
                'Error rendering `{template}` -> {error}',
128
                ['template' => $templatePath, 'error' => $e->getMessage()]
129
            );
130
            Craft::error($htmlText, __METHOD__);
131
        }
132
133
        // Restore the old template mode
134
        try {
135
            Craft::$app->view->setTemplateMode($oldMode);
136
        } catch (Exception $e) {
137
            Craft::error($e->getMessage(), __METHOD__);
138
        }
139
140
        return Template::raw($htmlText);
141
    }
142
}
143