Passed
Push — v1 ( 887199...d7f64b )
by Andrew
17:42 queued 08:53
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
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * Webperf plugin for Craft CMS 3.x
4
 *
5
 * Monitor the performance of your webpages through real-world user timing data
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) 2018 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\webperf\helpers;
12
13
use nystudio107\webperf\Webperf;
14
15
use Craft;
0 ignored issues
show
Bug introduced by
The type Craft 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...
16
use craft\helpers\Template;
0 ignored issues
show
Bug introduced by
The type craft\helpers\Template 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
use craft\web\View;
0 ignored issues
show
Bug introduced by
The type craft\web\View 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...
18
use yii\base\Exception;
0 ignored issues
show
Bug introduced by
The type yii\base\Exception 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...
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @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 indented incorrectly; expected 2 spaces but found 4
Loading history...
22
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
23
 * @since     1.0.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 indented incorrectly; expected 3 spaces but found 5
Loading history...
24
 */
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...
25
class PluginTemplate
26
{
27
    // Static Methods
28
    // =========================================================================
29
30
    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...
31
    {
32
        try {
33
            $html = Craft::$app->getView()->renderString($templateString, $params);
34
        } catch (\Exception $e) {
35
            $html = Craft::t(
36
                'webperf',
37
                'Error rendering template string -> {error}',
38
                ['error' => $e->getMessage()]
39
            );
40
            Craft::error($html, __METHOD__);
41
        }
42
43
        return $html;
44
    }
45
46
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $templatePath should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $params should have a doc-comment as per coding-style.
Loading history...
47
     * Render a plugin template
48
     *
49
     * @param $templatePath
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
50
     * @param $params
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
51
     *
52
     * @return string
53
     */
54
    public static function renderPluginTemplate(string $templatePath, array $params = []): string
55
    {
56
        // Stash the old template mode, and set it Control Panel template mode
57
        $oldMode = Craft::$app->view->getTemplateMode();
58
        try {
59
            Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP);
60
        } catch (Exception $e) {
61
            Craft::error($e->getMessage(), __METHOD__);
62
        }
63
64
        // Render the template with our vars passed in
65
        try {
66
            $htmlText = Craft::$app->view->renderTemplate('webperf/' . $templatePath, $params);
67
            $templateRendered = true;
68
        } catch (\Exception $e) {
69
            $htmlText = Craft::t(
70
                'webperf',
71
                'Error rendering `{template}` -> {error}',
72
                ['template' => $templatePath, 'error' => $e->getMessage()]
73
            );
74
            Craft::error($htmlText, __METHOD__);
75
            $templateRendered = false;
76
        }
77
78
        // If we couldn't find a plugin template, look for a frontend template
79
        if (!$templateRendered) {
80
            try {
81
                Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE);
82
            } catch (Exception $e) {
83
                Craft::error($e->getMessage(), __METHOD__);
84
            }
85
            // Render the template with our vars passed in
86
            try {
87
                $htmlText = Craft::$app->view->renderTemplate($templatePath, $params);
88
                $templateRendered = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $templateRendered is dead and can be removed.
Loading history...
89
            } catch (\Exception $e) {
90
                $htmlText = Craft::t(
91
                    'webperf',
92
                    'Error rendering `{template}` -> {error}',
93
                    ['template' => $templatePath, 'error' => $e->getMessage()]
94
                );
95
                Craft::error($htmlText, __METHOD__);
96
                $templateRendered = false;
97
            }
98
        }
99
100
        // Restore the old template mode
101
        try {
102
            Craft::$app->view->setTemplateMode($oldMode);
103
        } catch (Exception $e) {
104
            Craft::error($e->getMessage(), __METHOD__);
105
        }
106
107
        return Template::raw($htmlText);
108
    }
109
}
110