Passed
Push — master ( 62880d...2f714f )
by Sebastian
07:50
created

Mailcode_Commands_Command_SetVariable   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 130
rs 10
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A validateSyntax_left() 0 14 2
A validateSyntax_right() 0 14 2
A validateSyntax_equals_sign() 0 21 3
A requiresParameters() 0 3 1
A getValidations() 0 7 1
A validateSyntax_split_parts() 0 12 2
A generatesContent() 0 3 1
A supportsType() 0 3 1
A getLabel() 0 3 1
A getName() 0 3 1
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Commands_Command_SetVariable} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see Mailcode_Commands_Command_SetVariable
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Mailcode command: set a variable value.
16
 *
17
 * @package Mailcode
18
 * @subpackage Commands
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Commands_Command_SetVariable extends Mailcode_Commands_Command_Type_Standalone
22
{
23
    const VALIDATION_MISSING_EQUALS_SIGN = 48501;
24
    const VALIDATION_NOT_SINGLE_EQUALS_SIGN = 48502;
25
    const VALIDATION_EMPTY_ASSIGNMENT = 48503;
26
    const VALIDATION_VARIABLE_LEFT_UNRECOGNIZED = 48504;
27
    const VALIDATION_ASSIGNMENT_STATEMENT_INVALID = 48505;
28
    
29
   /**
30
    * @var string[]
31
    */
32
    protected $parts = array();
33
    
34
   /**
35
    * @var Mailcode_Variables_Variable
36
    */
37
    protected $leftVar;
38
    
39
   /**
40
    * @var Mailcode_Parser_Statement
41
    */
42
    protected $statement;
43
    
44
    public function getName() : string
45
    {
46
        return 'setvar';
47
    }
48
    
49
    public function getLabel() : string
50
    {
51
        return t('Set variable value');
52
    }
53
    
54
    public function supportsType(): bool
55
    {
56
        return false;
57
    }
58
    
59
    public function requiresParameters(): bool
60
    {
61
        return true;
62
    }
63
    
64
    protected function validateSyntax_equals_sign()
65
    {
66
        $amount = substr_count($this->paramsString, '=');
67
        
68
        if($amount === 1)
69
        {
70
            return;
71
        }
72
        
73
        if($amount < 1)
74
        {
75
            $this->validationResult->makeError(
76
                t('The quality operator (=) is missing.'),
77
                self::VALIDATION_MISSING_EQUALS_SIGN
78
            );
79
        }
80
        else
81
        {
82
            $this->validationResult->makeError(
83
                t('Only a single equality operator (=) should be used for variable assignment.'),
84
                self::VALIDATION_NOT_SINGLE_EQUALS_SIGN
85
            );
86
        }
87
    }
88
    
89
    protected function validateSyntax_split_parts()
90
    {
91
        $this->parts = \AppUtils\ConvertHelper::explodeTrim('=', $this->paramsString);
92
        
93
        if(count($this->parts) === 2) 
94
        {
95
            return;
96
        }
97
        
98
        $this->validationResult->makeError(
99
            t('Command has an empty assignment on either part of the equals sign.'),
100
            self::VALIDATION_EMPTY_ASSIGNMENT
101
        );
102
    }
103
    
104
    protected function validateSyntax_left()
105
    {
106
        // any variables we may find have already been validated.
107
        $vars = $this->mailcode->findVariables($this->parts[0])->getGroupedByName();
108
        
109
        if(count($vars) === 1)
110
        {
111
            $this->leftVar = array_shift($vars);
112
            return;
113
        }
114
        
115
        $this->validationResult->makeError(
116
            t('The name of the variable being set, %1$s, could not be recognized.', '"'.$this->parts[0].'"'),
117
            self::VALIDATION_VARIABLE_LEFT_UNRECOGNIZED
118
        );
119
    }
120
    
121
    protected function validateSyntax_right()
122
    {
123
        $this->statement = $this->mailcode->getParser()->createStatement($this->parts[1]);
124
        
125
        if($this->statement->isValid())
126
        {
127
            return;
128
        }
129
        
130
        $result = $this->statement->getValidationResult();
131
        
132
        $this->validationResult->makeError(
133
            t('The assignment statement is invalid:').' '.$result->getErrorMessage(),
134
            self::VALIDATION_ASSIGNMENT_STATEMENT_INVALID
135
        );
136
    }
137
    
138
    protected function getValidations() : array
139
    {
140
        return array(
141
            'equals_sign', 
142
            'split_parts',
143
            'left',
144
            'right'
145
        );
146
    }
147
    
148
    public function generatesContent() : bool
149
    {
150
        return true;
151
    }
152
}
153