Passed
Push — master ( 6c661c...e8275e )
by Sebastian
04:14
created

Mailcode::createPreProcessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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
    public function createString(string $subject) : Mailcode_StringContainer
106
    {
107
        return new Mailcode_StringContainer($subject);
108
    }
109
110
    /**
111
     * Attempts to find all variables in the target string.
112
     *
113
     * @param string $subject
114
     * @param Mailcode_Commands_Command|null $sourceCommand
115
     * @return Mailcode_Variables_Collection_Regular
116
     */
117
    public function findVariables(string $subject, ?Mailcode_Commands_Command $sourceCommand=null) : Mailcode_Variables_Collection_Regular
118
    {
119
        return $this->createVariables()->parseString($subject, $sourceCommand);
120
    }
121
    
122
    public function createVariables() : Mailcode_Variables
123
    {
124
        if(!isset($this->variables))
125
        {
126
            $this->variables = new Mailcode_Variables();
127
        }
128
        
129
        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...
130
    }
131
    
132
   /**
133
    * Creates the translator, which can be used to convert commands
134
    * to another supported syntax.
135
    * 
136
    * @return Mailcode_Translator
137
    */
138
    public function createTranslator() : Mailcode_Translator
139
    {
140
        if(!isset($this->translator))
141
        {
142
            $this->translator = new Mailcode_Translator(); 
143
        }
144
        
145
        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...
146
    }
147
    
148
   /**
149
    * Creates the styler, which can be used to retrieve the 
150
    * CSS required to style the highlighted commands in HTML.
151
    * 
152
    * @return Mailcode_Styler
153
    */
154
    public function createStyler() : Mailcode_Styler
155
    {
156
        return new Mailcode_Styler();
157
    }
158
159
    /**
160
     * Creates a new pre processor instance for the specified content
161
     * string, to replace all pre process enabled commands with their
162
     * corresponding contents.
163
     *
164
     * @param string $subject
165
     * @return Mailcode_PreProcessor
166
     */
167
    public function createPreProcessor(string $subject) : Mailcode_PreProcessor
168
    {
169
        return new Mailcode_PreProcessor($subject);
170
    }
171
}
172