PluginTemplate::renderPluginTemplate()   B
last analyzed

Complexity

Conditions 7
Paths 40

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 35
c 1
b 0
f 0
nc 40
nop 2
dl 0
loc 54
rs 8.4266

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * YouTube Live Embed plugin for Craft CMS
4
 *
5
 * This plugin allows you to embed a YouTube live stream and/or live chat on your webpage
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) 2019 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\youtubeliveembed\helpers;
12
13
use Craft;
14
use craft\helpers\Template;
15
use craft\web\View;
16
use Twig\Markup;
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   YoutubeLiveEmbed
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.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 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
    // Static Methods
27
    // =========================================================================
28
29
    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...
30
    {
31
        try {
32
            $html = Craft::$app->getView()->renderString($templateString, $params);
33
        } catch (\Exception $e) {
34
            $html = Craft::t(
35
                'youtubeliveembed',
36
                'Error rendering template string -> {error}',
37
                ['error' => $e->getMessage()]
38
            );
39
            Craft::error($html, __METHOD__);
40
        }
41
42
        return $html;
43
    }
44
45
    /**
46
     * Render a plugin template
47
     *
48
     * @param $templatePath
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
     * @param $params
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
50
     *
51
     * @return Markup
52
     */
53
    public static function renderPluginTemplate(string $templatePath, array $params = []): Markup
54
    {
55
        // Stash the old template mode, and set it Control Panel template mode
56
        $oldMode = Craft::$app->view->getTemplateMode();
57
        try {
58
            Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP);
59
        } catch (Exception $e) {
60
            Craft::error($e->getMessage(), __METHOD__);
61
        }
62
63
        // Render the template with our vars passed in
64
        try {
65
            $htmlText = Craft::$app->view->renderTemplate('youtubeliveembed/' . $templatePath, $params);
66
            $templateRendered = true;
67
        } catch (\Exception $e) {
68
            $htmlText = Craft::t(
69
                'youtubeliveembed',
70
                'Error rendering `{template}` -> {error}',
71
                ['template' => $templatePath, 'error' => $e->getMessage()]
72
            );
73
            Craft::error($htmlText, __METHOD__);
74
            $templateRendered = false;
75
        }
76
77
        // If we couldn't find a plugin template, look for a frontend template
78
        if (!$templateRendered) {
79
            try {
80
                Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE);
81
            } catch (Exception $e) {
82
                Craft::error($e->getMessage(), __METHOD__);
83
            }
84
            // Render the template with our vars passed in
85
            try {
86
                $htmlText = Craft::$app->view->renderTemplate($templatePath, $params);
87
                $templateRendered = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $templateRendered is dead and can be removed.
Loading history...
88
            } catch (\Exception $e) {
89
                $htmlText = Craft::t(
90
                    'youtubeliveembed',
91
                    'Error rendering `{template}` -> {error}',
92
                    ['template' => $templatePath, 'error' => $e->getMessage()]
93
                );
94
                Craft::error($htmlText, __METHOD__);
95
                $templateRendered = false;
96
            }
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