Passed
Push — master ( d3640a...6ab608 )
by Sebastian
02:29
created

getVariable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 13
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Commands_Command_ShowSnippet} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see Mailcode_Commands_Command_ShowSnippet
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Mailcode command: show a variable value.
16
 *
17
 * @package Mailcode
18
 * @subpackage Commands
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Commands_Command_ShowSnippet extends Mailcode_Commands_Command_Type_Standalone
22
{
23
    const ERROR_NO_VARIABLE_AVAILABLE = 51901;
24
    
25
    const VALIDATION_VARIABLE_COUNT_MISMATCH = 52001;
26
    
27
   /**
28
    * @var Mailcode_Variables_Variable|NULL
29
    */
30
    protected $variable;
31
    
32
    public function getName() : string
33
    {
34
        return 'showsnippet';
35
    }
36
    
37
    public function getLabel() : string
38
    {
39
        return t('Show text snippet');
40
    }
41
    
42
    public function supportsType(): bool
43
    {
44
        return false;
45
    }
46
47
    public function requiresParameters(): bool
48
    {
49
        return true;
50
    }
51
    
52
   /**
53
    * Retrieves the variable to show.
54
    * 
55
    * NOTE: Only available once the command has been
56
    * validated. Always use isValid() first.
57
    * 
58
    * @throws Mailcode_Exception
59
    * @return Mailcode_Variables_Variable
60
    */
61
    public function getVariable() : Mailcode_Variables_Variable
62
    {
63
        $this->validate();
64
        
65
        if(isset($this->variable))
66
        {
67
            return $this->variable;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->variable could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Variables_Variable. Consider adding an additional type-check to rule them out.
Loading history...
68
        }
69
        
70
        throw new Mailcode_Exception(
71
            'No variable available.',
72
            'No variable is present at this time, or the validation failed.',
73
            self::ERROR_NO_VARIABLE_AVAILABLE
74
        );
75
    }
76
    
77
   /**
78
    * Retrieves the full name of the variable to show.
79
    * 
80
    * NOTE: Only available once the command has been
81
    * validated. Always use isValid() first.
82
    * 
83
    * @throws Mailcode_Exception
84
    * @return string
85
    */
86
    public function getVariableName() : string
87
    {
88
        return $this->getVariable()->getFullName();
89
    }
90
91
    protected function validateSyntax_require_variable() : void
92
    {
93
         $vars = $this->getVariables()->getGroupedByName();
94
         $amount = count($vars);
95
         
96
         if($amount === 1)
97
         {
98
             $this->variable = array_pop($vars);
99
             return;
100
         }
101
         
102
         $this->validationResult->makeError(
103
            t('Command has %1$s variables, %2$s expected.', $amount, 1),
104
            self::VALIDATION_VARIABLE_COUNT_MISMATCH
105
         );
106
    }
107
    
108
    protected function getValidations() : array
109
    {
110
        return array('require_variable');
111
    }
112
    
113
    public function generatesContent() : bool
114
    {
115
        return true;
116
    }
117
}
118