Passed
Push — master ( 138b71...c5c69e )
by Sebastian
04:58
created

Mailcode_Factory_Instantiator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 121
rs 10
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A buildIfVar() 0 15 2
A buildIf() 0 29 3
A exceptionUnexpectedType() 0 8 1
A buildIfContains() 0 17 2
A quoteString() 0 3 1
A checkCommand() 0 13 2
A filterVariableName() 0 5 1
A buildIfEmpty() 0 3 1
A buildIfNotEmpty() 0 3 1
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Factory_Instantiator} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Utilities
7
 * @see Mailcode_Factory_Instantiator
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Factory utility used to create commands.
16
 *
17
 * @package Mailcode
18
 * @subpackage Utilities
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Factory_Instantiator
22
{
23
    public function buildIf(string $ifType, string $params, string $type='') : Mailcode_Commands_IfBase
24
    {
25
        $stringType = $type;
26
        
27
        if(!empty($type))
28
        {
29
            $stringType = ' '.$type;
30
        }
31
        
32
        $command = Mailcode::create()->getCommands()->createCommand(
33
            $ifType,
34
            $type,
35
            $params,
36
            sprintf(
37
                '{%s%s: %s}',
38
                strtolower($ifType),
39
                $stringType,
40
                $params
41
            )
42
        );
43
        
44
        $this->checkCommand($command);
45
        
46
        if($command instanceof Mailcode_Commands_IfBase)
47
        {
48
            return $command;
49
        }
50
        
51
        throw $this->exceptionUnexpectedType('IfBase', $command);
52
    }
53
    
54
    public function buildIfVar(string $ifType, string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_IfBase
55
    {
56
        if($quoteValue)
57
        {
58
            $value = $this->quoteString($value);
59
        }
60
        
61
        $condition = sprintf(
62
            "%s %s %s",
63
            $this->filterVariableName($variable),
64
            $operand,
65
            $value
66
        );
67
        
68
        return $this->buildIf($ifType, $condition, 'variable');
69
    }
70
    
71
    public function buildIfEmpty(string $ifType, string $variable) : Mailcode_Commands_IfBase
72
    {
73
        return $this->buildIf($ifType, $this->filterVariableName($variable), 'empty');
74
    }
75
    
76
    public function buildIfNotEmpty(string $ifType, string $variable) : Mailcode_Commands_IfBase
77
    {
78
        return $this->buildIf($ifType, $this->filterVariableName($variable), 'not-empty');
79
    }
80
    
81
    public function buildIfContains(string $ifType, string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_IfBase
82
    {
83
        $keyword = ' ';
84
        
85
        if($caseInsensitive)
86
        {
87
            $keyword = ' insensitive: ';
88
        }
89
        
90
        $condition = sprintf(
91
            '%s%s"%s"',
92
            $this->filterVariableName($variable),
93
            $keyword,
94
            $search
95
        );
96
        
97
        return $this->buildIf($ifType, $condition, 'contains');
98
    }
99
    
100
    public function filterVariableName(string $name) : string
101
    {
102
        $name = preg_replace('/\s/', '', $name);
103
        
104
        return '$'.ltrim($name, '$');
105
    }
106
    
107
    public function checkCommand(Mailcode_Commands_Command $command) : void
108
    {
109
        if($command->isValid())
110
        {
111
            return;
112
        }
113
        
114
        throw new Mailcode_Factory_Exception(
115
            'Invalid command created.',
116
            'Validation message: '.$command->getValidationResult()->getErrorMessage(),
117
            Mailcode_Factory::ERROR_INVALID_COMMAND_CREATED,
118
            null,
119
            $command
120
        );
121
    }
122
    
123
    /**
124
     * Quotes a string literal: adds the quotes, and escapes any quotes already present in it.
125
     *
126
     * @param string $string
127
     * @return string
128
     */
129
    public function quoteString(string $string) : string
130
    {
131
        return '"'.str_replace('"', '\"', $string).'"';
132
    }
133
    
134
    public function exceptionUnexpectedType(string $type, Mailcode_Commands_Command $command) : Mailcode_Factory_Exception
135
    {
136
        return new Mailcode_Factory_Exception(
137
            'Invalid command class type created.',
138
            sprintf('Excepted type [%s], but created class [%s].', $type, get_class($command)),
139
            Mailcode_Factory::ERROR_UNEXPECTED_COMMAND_TYPE,
140
            null,
141
            $command
142
        );
143
    }
144
}
145