Passed
Push — develop ( 513a12...4e153d )
by Andrew
09:49
created

TwigExpressionValidator::validateAttribute()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 6
eloc 20
c 4
b 1
f 0
nc 16
nop 2
dl 0
loc 29
ccs 0
cts 27
cp 0
crap 42
rs 8.9777
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 Craft;
15
use Exception;
16
use nystudio107\seomatic\Seomatic;
17
use nystudio107\seomatic\variables\SeomaticVariable;
18
use yii\base\Model;
19
use yii\validators\Validator;
20
use function is_string;
21
22
/**
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...
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
                    Seomatic::$plugin->metaContainers->loadGlobalMetaContainers();
45
                    Seomatic::$seomaticVariable->init();
46
                }
47
                Craft::$app->getView()->renderString($value, [
48
                    'seomatic' => Seomatic::$seomaticVariable
49
                ]);
50
            } catch (Exception $e) {
51
                $error = Craft::t(
52
                    'seomatic',
53
                    'Error rendering template string -> {error}',
54
                    ['error' => $e->getMessage()]
55
                );
56
            }
57
        } else {
58
            $error = Craft::t('seomatic', 'Is not a string.');
59
        }
60
        // If there's an error, add it to the model, and log it
61
        if ($error) {
62
            $model->addError($attribute, $error);
63
            Craft::error($error, __METHOD__);
64
        }
65
    }
66
}
67