Passed
Push — v3 ( 1e51d3...178405 )
by Andrew
25:46
created

TwigExpressionValidator::validateAttribute()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 18
c 3
b 0
f 0
nc 12
nop 2
dl 0
loc 27
ccs 0
cts 25
cp 0
crap 42
rs 9.0444
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\validators;
13
14
use nystudio107\seomatic\Seomatic;
15
use nystudio107\seomatic\variables\SeomaticVariable;
16
17
use Craft;
18
19
use yii\base\Model;
20
use yii\validators\Validator;
21
22
/**
23
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
24
 * @package   Seomatic
25
 * @since     3.4.0
26
 */
27
class TwigExpressionValidator extends Validator
28
{
29
    // Public Methods
30
    // =========================================================================
31
32
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $attribute should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $model should have a doc-comment as per coding-style.
Loading history...
33
     * @inheritdoc
34
     */
35
    public function validateAttribute($model, $attribute)
36
    {
37
        /** @var Model $model */
0 ignored issues
show
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
38
        $value = $model->$attribute;
39
        $error = null;
40
        if (!empty($value) && \is_string($value)) {
41
            try {
42
                if (Seomatic::$seomaticVariable === null) {
43
                    Seomatic::$seomaticVariable = new SeomaticVariable();
44
                }
45
                Craft::$app->getView()->renderString($value, [
46
                    'seomatic' => Seomatic::$seomaticVariable
47
                ]);
48
            } catch (\Exception $e) {
49
                $error = Craft::t(
50
                    'seomatic',
51
                    'Error rendering template string -> {error}',
52
                    ['error' => $e->getMessage()]
53
                );
54
            }
55
        } else {
56
            $error = Craft::t('seomatic', 'Is not a string.');
57
        }
58
        // If there's an error, add it to the model, and log it
59
        if ($error) {
60
            $model->addError($attribute, $error);
61
            Craft::error($error, __METHOD__);
62
        }
63
    }
64
}
65