EditableTemplate::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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\models;
13
14
use Craft;
15
use nystudio107\codeeditor\validators\TwigTemplateValidator;
16
use nystudio107\seomatic\base\FrontendTemplate;
17
use nystudio107\seomatic\helpers\PluginTemplate as PluginTemplateHelper;
18
19
/**
20
 * @author    nystudio107
21
 * @package   Seomatic
22
 * @since     3.0.0
23
 */
24
class EditableTemplate extends FrontendTemplate
25
{
26
    // Constants
27
    // =========================================================================
28
29
    const TEMPLATE_TYPE = 'EditableTemplate';
30
31
    // Static Methods
32
    // =========================================================================
33
34
    /**
35
     * @param array $config
36
     *
37
     * @return null|EditableTemplate
38
     */
39
    public static function create(array $config = [])
40
    {
41
        $model = new EditableTemplate($config);
42
        // Load $templateString from the source template if it's not set
43
        if (empty($model->templateString)) {
44
            $model->loadTemplate();
45
        }
46
47
        return $model;
48
    }
49
50
51
    // Public Properties
52
    // =========================================================================
53
54
    /**
55
     * @var string
56
     * @deprecated This is no longer used
57
     */
58
    public $templateVersion = '1.0.0';
59
60
    /**
61
     * The template to render this FrontendTemplate
62
     *
63
     * @var string
64
     */
65
    public $templateString;
66
67
    /**
68
     * @var int
69
     */
70
    public $siteId;
71
72
    // Public Methods
73
    // =========================================================================
74
75
    /**
76
     * Load the existing template into a string
77
     */
78
    public function loadTemplate()
79
    {
80
        $this->templateString = '';
81
        // First try it from the Craft template directory
82
        $path = Craft::getAlias('@templates/')
0 ignored issues
show
Bug introduced by
Are you sure Craft::getAlias('@templates/') of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        $path = /** @scrutinizer ignore-type */ Craft::getAlias('@templates/')
Loading history...
83
            . $this->template;
84
        if (file_exists($path)) {
85
            $this->templateString = @file_get_contents($path);
0 ignored issues
show
Documentation Bug introduced by
It seems like @file_get_contents($path) can also be of type false. However, the property $templateString is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
86
        } else {
87
            // Next try from our plugin directory first
88
            $path = Craft::getAlias('@nystudio107/seomatic/templates/')
0 ignored issues
show
Bug introduced by
Are you sure Craft::getAlias('@nystud...7/seomatic/templates/') of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
            $path = /** @scrutinizer ignore-type */ Craft::getAlias('@nystudio107/seomatic/templates/')
Loading history...
89
                . $this->template;
90
            if (file_exists($path)) {
91
                $this->templateString = @file_get_contents($path);
92
            }
93
        }
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public function rules(): array
100
    {
101
        $rules = parent::rules();
102
        $rules = array_merge($rules, [
103
            [['templateString'], 'required'],
104
            [['templateString'], 'string'],
105
            [['templateString'], TwigTemplateValidator::class],
106
        ]);
107
108
        return $rules;
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    public function render(array $params = []): string
115
    {
116
        return PluginTemplateHelper::renderStringTemplate($this->templateString);
117
    }
118
}
119