Passed
Push — v1 ( 17b0b9...5234ef )
by Andrew
14:55 queued 06:40
created

PluginTemplate::renderStringTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 14
rs 9.9666
1
<?php
2
/**
3
 * Recipe plugin for Craft CMS 3.x
4
 *
5
 * A comprehensive recipe FieldType for Craft CMS that includes metric/imperial
6
 * conversion, portion calculation, and JSON-LD microdata support
7
 *
8
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2020 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
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...
11
12
namespace nystudio107\recipe\helpers;
13
14
use Craft;
15
use craft\helpers\Template;
16
use craft\web\View;
17
18
use yii\base\Exception;
19
20
use Twig\Markup;
21
22
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
 * @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...
24
 * @package   Recipe
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
25
 * @since     1.1.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...
26
 */
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...
27
class PluginTemplate
28
{
29
    // Static Methods
30
    // =========================================================================
31
32
    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...
33
    {
34
        try {
35
            $html = Craft::$app->getView()->renderString($templateString, $params);
36
        } catch (\Exception $e) {
37
            $html = Craft::t(
38
                'recipe',
39
                'Error rendering template string -> {error}',
40
                ['error' => $e->getMessage()]
41
            );
42
            Craft::error($html, __METHOD__);
43
        }
44
45
        return $html;
46
    }
47
48
    /**
49
     * Render a plugin template
50
     *
51
     * @param $templatePath
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
52
     * @param $params
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
53
     *
54
     * @return Markup
55
     */
56
    public static function renderPluginTemplate(string $templatePath, array $params = []): Markup
57
    {
58
        $htmlText = '';
59
        // Stash the old template mode, and set it Control Panel template mode
60
        $oldMode = Craft::$app->view->getTemplateMode();
61
        $templateRendered = false;
62
        // Look for a frontend template to render first
63
        if (!$templateRendered) {
0 ignored issues
show
introduced by
The condition $templateRendered is always false.
Loading history...
64
            try {
65
                Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE);
66
            } catch (Exception $e) {
67
                Craft::error($e->getMessage(), __METHOD__);
68
            }
69
            // Render the template with our vars passed in
70
            try {
71
                $htmlText = Craft::$app->view->renderTemplate('recipe/' .$templatePath, $params);
72
                $templateRendered = true;
73
            } catch (\Exception $e) {
74
                $htmlText = Craft::t(
75
                    'recipe',
76
                    'Error rendering `{template}` -> {error}',
77
                    ['template' => $templatePath, 'error' => $e->getMessage()]
78
                );
79
                Craft::error($htmlText, __METHOD__);
80
                $templateRendered = false;
81
            }
82
        }
83
        // If no frontend template was found, try our built-in template
84
        if (!$templateRendered) {
85
            try {
86
                Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP);
87
            } catch (Exception $e) {
88
                Craft::error($e->getMessage(), __METHOD__);
89
            }
90
            // Render the template with our vars passed in
91
            try {
92
                $htmlText = Craft::$app->view->renderTemplate('recipe/' . $templatePath, $params);
93
                $templateRendered = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $templateRendered is dead and can be removed.
Loading history...
94
            } catch (\Exception $e) {
95
                $htmlText = Craft::t(
96
                    'recipe',
97
                    'Error rendering `{template}` -> {error}',
98
                    ['template' => $templatePath, 'error' => $e->getMessage()]
99
                );
100
                Craft::error($htmlText, __METHOD__);
101
                $templateRendered = false;
102
            }
103
        }
104
105
        // Restore the old template mode
106
        try {
107
            Craft::$app->view->setTemplateMode($oldMode);
108
        } catch (Exception $e) {
109
            Craft::error($e->getMessage(), __METHOD__);
110
        }
111
112
        return Template::raw($htmlText);
113
    }
114
}
115