Test Failed
Push — master ( d4e985...d9cf0c )
by Sebastian
03:32
created

Mailcode::createString()   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
use Psr\Log\LoggerInterface;
15
16
/**
17
 * Main hub for the "Mailcode" syntax handling, which is used
18
 * to abstract the actual command syntax used by the selected
19
 * mailing format.
20
 * 
21
 * Users only work with the mailcode commands to ensure that
22
 * the mail editor interface stays independent of the actual
23
 * format implementation used by the backend systems.
24
 *
25
 * @package Mailcode
26
 * @subpackage Core
27
 * @author Sebastian Mordziol <[email protected]>
28
 */
29
class Mailcode
30
{
31
    public const PACKAGE_NAME = 'Mailcode';
32
33
    /**
34
    * @var Mailcode_Parser|NULL
35
    */
36
    protected $parser = null;
37
    
38
   /**
39
    * @var Mailcode_Commands|NULL
40
    */
41
    protected $commands = null;
42
    
43
   /**
44
    * @var Mailcode_Variables|NULL
45
    */
46
    protected $variables = null;
47
    
48
   /**
49
    * @var Mailcode_Translator|NULL
50
    */
51
    protected $translator = null;
52
    
53
   /**
54
    * Creates a new mailcode instance.
55
    * @return Mailcode
56
    */
57
    public static function create() : Mailcode
58
    {
59
        return new Mailcode();
60
    }
61
62
    public static function getName() : string
63
    {
64
        return self::PACKAGE_NAME;
65
    }
66
67
    /**
68
    * Parses the string to detect all commands contained within.
69
    * 
70
    * @param string $string
71
    * @return Mailcode_Collection
72
    */
73
    public function parseString(string $string) : Mailcode_Collection
74
    {
75
        return $this->getParser()
76
            ->parseString($string)
77
            ->getCollection();
78
    }
79
    
80
   /**
81
    * Retrieves the string parser instance used to detect commands.
82
    * 
83
    * @return Mailcode_Parser
84
    */
85
    public function getParser() : Mailcode_Parser
86
    {
87
        if(!isset($this->parser)) 
88
        {
89
            $this->parser = new Mailcode_Parser($this);
90
        }
91
        
92
        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...
93
    }
94
    
95
   /**
96
    * Retrieves the commands collection, which is used to
97
    * access information on the available commands.
98
    * 
99
    * @return Mailcode_Commands
100
    */
101
    public function getCommands() : Mailcode_Commands
102
    {
103
        if(!isset($this->commands)) 
104
        {
105
            $this->commands = new Mailcode_Commands();
106
        }
107
        
108
        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...
109
    }
110
    
111
    public function createSafeguard(string $subject) : Mailcode_Parser_Safeguard
112
    {
113
        return $this->getParser()->createSafeguard($subject);
114
    }
115
    
116
    public function createString(string $subject) : Mailcode_StringContainer
117
    {
118
        return new Mailcode_StringContainer($subject);
119
    }
120
121
    /**
122
     * Attempts to find all variables in the target string.
123
     *
124
     * @param string $subject
125
     * @param Mailcode_Commands_Command|null $sourceCommand
126
     * @return Mailcode_Variables_Collection_Regular
127
     */
128
    public function findVariables(string $subject, ?Mailcode_Commands_Command $sourceCommand=null) : Mailcode_Variables_Collection_Regular
129
    {
130
        return $this->createVariables()->parseString($subject, $sourceCommand);
131
    }
132
    
133
    public function createVariables() : Mailcode_Variables
134
    {
135
        if(!isset($this->variables))
136
        {
137
            $this->variables = new Mailcode_Variables();
138
        }
139
        
140
        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...
141
    }
142
    
143
   /**
144
    * Creates the translator, which can be used to convert commands
145
    * to another supported syntax.
146
    * 
147
    * @return Mailcode_Translator
148
    */
149
    public function createTranslator() : Mailcode_Translator
150
    {
151
        if(!isset($this->translator))
152
        {
153
            $this->translator = new Mailcode_Translator(); 
154
        }
155
        
156
        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...
157
    }
158
    
159
   /**
160
    * Creates the styler, which can be used to retrieve the 
161
    * CSS required to style the highlighted commands in HTML.
162
    * 
163
    * @return Mailcode_Styler
164
    */
165
    public function createStyler() : Mailcode_Styler
166
    {
167
        return new Mailcode_Styler();
168
    }
169
170
    /**
171
     * Creates a new pre-processor instance for the specified content
172
     * string, to replace all pre-process enabled commands with their
173
     * corresponding contents.
174
     *
175
     * @param string $subject
176
     * @return Mailcode_PreProcessor
177
     */
178
    public function createPreProcessor(string $subject) : Mailcode_PreProcessor
179
    {
180
        return new Mailcode_PreProcessor($subject);
181
    }
182
183
    /**
184
     * @var LoggerInterface|NULL
185
     */
186
    private static ?LoggerInterface $logger = null;
187
    private static bool $debug = false;
188
189
    public static function setLogger(LoggerInterface $logger) : void
190
    {
191
        self::$logger = $logger;
192
    }
193
194
    public static function getLogger() : ?LoggerInterface
195
    {
196
        return self::$logger;
197
    }
198
199
    public static function setDebugging(bool $enabled=true) : void
200
    {
201
        self::$debug = $enabled;
202
    }
203
204
    public static function isDebugEnabled() : bool
205
    {
206
        return self::$debug;
207
    }
208
209
    /**
210
     * @param string $message
211
     * @param array<string|int,mixed> $context
212
     * @return void
213
     */
214
    public static function debug(string $message, array $context=array()) : void
215
    {
216
        if(self::$debug && isset(self::$logger))
217
        {
218
            self::$logger->debug($message, $context);
219
        }
220
    }
221
}
222