Passed
Push — master ( d06d8f...13736a )
by Sebastian
02:30
created

Mailcode::createTranslator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Core
7
 * @see Mailcode
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Main hub for the "Mailcode" syntax handling, which is used
16
 * to abstract the actual commands syntax used by the selected
17
 * mailing format.
18
 * 
19
 * Users only work with the mailcode commands to ensure that
20
 * the mail editor interface stays independent of the actual
21
 * format implementation used by the backend systems.
22
 *
23
 * @package Mailcode
24
 * @subpackage Core
25
 * @author Sebastian Mordziol <[email protected]>
26
 */
27
class Mailcode
28
{
29
   /**
30
    * @var Mailcode_Parser|NULL
31
    */
32
    protected $parser = null;
33
    
34
   /**
35
    * @var Mailcode_Commands|NULL
36
    */
37
    protected $commands = null;
38
    
39
   /**
40
    * @var Mailcode_Variables|NULL
41
    */
42
    protected $variables = null;
43
    
44
   /**
45
    * @var Mailcode_Translator|NULL
46
    */
47
    protected $translator = null;
48
    
49
   /**
50
    * Creates a new mailcode instance.
51
    * @return Mailcode
52
    */
53
    public static function create() : Mailcode
54
    {
55
        return new Mailcode();
56
    }
57
    
58
   /**
59
    * Parses the string to detect all commands contained within.
60
    * 
61
    * @param string $string
62
    * @return Mailcode_Collection
63
    */
64
    public function parseString(string $string) : Mailcode_Collection
65
    {
66
        return $this->getParser()->parseString($string);
67
    }
68
    
69
   /**
70
    * Retrieves the string parser instance used to detect commands.
71
    * 
72
    * @return Mailcode_Parser
73
    */
74
    public function getParser() : Mailcode_Parser
75
    {
76
        if(!isset($this->parser)) 
77
        {
78
            $this->parser = new Mailcode_Parser($this);
79
        }
80
        
81
        return $this->parser;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->parser could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Parser. Consider adding an additional type-check to rule them out.
Loading history...
82
    }
83
    
84
   /**
85
    * Retrieves the commands collection, which is used to
86
    * access information on the available commands.
87
    * 
88
    * @return Mailcode_Commands
89
    */
90
    public function getCommands() : Mailcode_Commands
91
    {
92
        if(!isset($this->commands)) 
93
        {
94
            $this->commands = new Mailcode_Commands();
95
        }
96
        
97
        return $this->commands;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->commands could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Commands. Consider adding an additional type-check to rule them out.
Loading history...
98
    }
99
    
100
    public function createSafeguard(string $subject) : Mailcode_Parser_Safeguard
101
    {
102
        return $this->getParser()->createSafeguard($subject);
103
    }
104
    
105
   /**
106
    * Attempts to find all variables in the target string.
107
    * 
108
    * @param string $subject
109
    * @return Mailcode_Variables_Collection_Regular
110
    */
111
    public function findVariables(string $subject) : Mailcode_Variables_Collection_Regular
112
    {
113
        return $this->createVariables()->parseString($subject);
114
    }
115
    
116
    public function createVariables() : Mailcode_Variables
117
    {
118
        if(!isset($this->variables))
119
        {
120
            $this->variables = new Mailcode_Variables();
121
        }
122
        
123
        return $this->variables;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->variables could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Variables. Consider adding an additional type-check to rule them out.
Loading history...
124
    }
125
    
126
   /**
127
    * Creates the translator, which can be used to convert commands
128
    * to another supported syntax.
129
    * 
130
    * @return Mailcode_Translator
131
    */
132
    public function createTranslator() : Mailcode_Translator
133
    {
134
        if(!isset($this->translator))
135
        {
136
            $this->translator = new Mailcode_Translator(); 
137
        }
138
        
139
        return $this->translator;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->translator could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Translator. Consider adding an additional type-check to rule them out.
Loading history...
140
    }
141
}
142