Mailcode_Factory_CommandSets_Set_Set::var()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 25
c 2
b 0
f 0
nc 6
nop 4
dl 0
loc 38
rs 9.52
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Factory_CommandSets_Set_Set} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Factory
7
 * @see Mailcode_Factory_CommandSets_Set_Set
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use AppUtils\ClassHelper;
15
use Mailcode\Interfaces\Commands\Validation\CountInterface;
16
17
/**
18
 * Factory utility used to create commands.
19
 *
20
 * @package Mailcode
21
 * @subpackage Factory
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
class Mailcode_Factory_CommandSets_Set_Set extends Mailcode_Factory_CommandSets_Set
25
{
26
    public function var(string $variableName, string $value, bool $quoteValue = true, bool $asCount = false): Mailcode_Commands_Command_SetVariable
27
    {
28
        $variableName = $this->instantiator->filterVariableName($variableName);
29
        $commandID = ClassHelper::getClassTypeName(Mailcode_Commands_Command_SetVariable::class);
30
31
        if ($asCount) {
32
            $params = sprintf(
33
                '%s %s=%s',
34
                $variableName,
35
                CountInterface::PARAMETER_NAME,
36
                $value
37
            );
38
        } else {
39
            if ($quoteValue) {
40
                $value = $this->instantiator->quoteString($value);
41
            }
42
43
            $params = $variableName . ' = ' . $value;
44
        }
45
46
        $cmd = $this->commands->createCommand(
47
            $commandID,
48
            '', // type
49
            $params,
50
            sprintf(
51
                '{%s: %s}',
52
                Mailcode_Commands_Command_SetVariable::COMMAND_NAME,
53
                $params
54
            )
55
        );
56
57
        $this->instantiator->checkCommand($cmd);
58
59
        if ($cmd instanceof Mailcode_Commands_Command_SetVariable) {
60
            return $cmd;
61
        }
62
63
        throw $this->instantiator->exceptionUnexpectedType($commandID, $cmd);
64
    }
65
66
    /**
67
     * Treats the value as a string literal, so automatically adds quotes around it.
68
     *
69
     * @param string $variableName
70
     * @param string $value
71
     * @return Mailcode_Commands_Command_SetVariable
72
     * @throws Mailcode_Factory_Exception
73
     */
74
    public function varString(string $variableName, string $value): Mailcode_Commands_Command_SetVariable
75
    {
76
        return $this->var($variableName, $value, true);
77
    }
78
79
    /**
80
     * Set a variable by counting the amount of entries in the
81
     * target list variable.
82
     *
83
     * @param string $variableName The name of the variable to store the value in.
84
     * @param string $listVariableName The name of the variable to count records of.
85
     * @return Mailcode_Commands_Command_SetVariable
86
     * @throws Mailcode_Factory_Exception
87
     */
88
    public function varCount(string $variableName, string $listVariableName) : Mailcode_Commands_Command_SetVariable
89
    {
90
        return $this->var(
91
            $variableName,
92
            dollarize($listVariableName),
93
            false,
94
            true
95
        );
96
    }
97
}
98