TwigObjectTemplateValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
c 2
b 0
f 0
dl 0
loc 69
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A validateAttribute() 0 27 5
1
<?php
2
/**
3
 * Twigfield for Craft CMS
4
 *
5
 * Provides a twig editor field with Twig & Craft API autocomplete
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) 2022 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\twigfield\validators;
12
13
use Craft;
14
use Exception;
15
use nystudio107\twigfield\events\RegisterTwigValidatorVariablesEvent;
16
use yii\base\Model;
17
use yii\validators\Validator;
18
use function is_string;
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @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...
22
 * @package   Twigfield
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
23
 * @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...
24
 */
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...
25
class TwigObjectTemplateValidator extends Validator
26
{
27
    // Constants
28
    // =========================================================================
29
30
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
     * @event RegisterTwigValidatorVariablesEvent The event that is triggered to allow
32
     *        you to register Variables to be passed down to the Twig context during
33
     *        Twig template validation
34
     *
35
     * ```php
36
     * use nystudio107\twigfield\validators\TwigTemplateValidator;
37
     * use nystudio107\twigfield\events\RegisterTwigValidatorVariablesEvent;
38
     * use yii\base\Event;
39
     *
40
     * Event::on(TwigTemplateValidator::class,
41
     *     TwigTemplateValidator::EVENT_REGISTER_TWIG_VALIDATOR_VARIABLES,
42
     *     function(RegisterTwigValidatorVariablesEvent $event) {
43
     *         $event->object = $myObject;
44
     *         $event->variables['variableName'] = $variableValue;
45
     *     }
46
     * );
47
     * ```
48
     */
49
    const EVENT_REGISTER_TWIG_VALIDATOR_VARIABLES = 'registerTwigValidatorVariables';
50
51
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
52
     * @var mixed The object that should be passed into `renderObjectTemplate()` during the template rendering
53
     */
54
    public $object = null;
55
56
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
57
     * @var array Variables in key => value format that should be available during the template rendering
58
     */
59
    public $variables = [];
60
61
    // Public Methods
62
    // =========================================================================
63
64
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $model should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $attribute should have a doc-comment as per coding-style.
Loading history...
65
     * @inheritdoc
66
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
67
    public function validateAttribute($model, $attribute)
68
    {
69
        /** @var Model $model */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
70
        $value = $model->$attribute;
71
        $error = null;
72
        if (!empty($value) && is_string($value)) {
73
            try {
74
                $event = new RegisterTwigValidatorVariablesEvent([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
75
                    'object' => $this->object,
76
                    'variables' => $this->variables,
77
                ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
78
                $this->trigger(self::EVENT_REGISTER_TWIG_VALIDATOR_VARIABLES, $event);
79
                Craft::$app->getView()->renderObjectTemplate($value, $event->object, $event->variables);
80
            } catch (Exception $e) {
81
                $error = Craft::t(
82
                    'twigfield',
83
                    'Error rendering template string -> {error}',
84
                    ['error' => $e->getMessage()]
85
                );
86
            }
87
        } else {
88
            $error = Craft::t('twigfield', 'Is not a string.');
89
        }
90
        // If there's an error, add it to the model, and log it
91
        if ($error) {
92
            $model->addError($attribute, $error);
93
            Craft::error($error, __METHOD__);
94
        }
95
    }
96
}
97