Passed
Push — master ( 2b422d...2c1f94 )
by Sebastian
03:35
created

Mailcode_Factory_CommandSets_Set_Set::varCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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