Price::compilePriceParams()   B
last analyzed

Complexity

Conditions 8
Paths 48

Size

Total Lines 46
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 46
rs 8.4444
cc 8
nc 48
nop 6
1
<?php
2
/**
3
 * @package Mailcode
4
 * @subpackage Factory
5
 */
6
7
declare(strict_types=1);
8
9
namespace Mailcode\Factory\CommandSets\Set\Show;
10
11
use Mailcode\CurrencyInterface;
12
use Mailcode\Mailcode_Commands_Command_ShowPrice;
13
use Mailcode\Mailcode_Commands_Keywords;
14
use Mailcode\Mailcode_Factory_CommandSets_Set;
15
use Mailcode\RegionInterface;
16
17
/**
18
 * Factory class for the `showprice` command.
19
 *
20
 * @package Mailcode
21
 * @subpackage Factory
22
 * @author Olaf Böcker <[email protected]>
23
 */
24
class Price extends Mailcode_Factory_CommandSets_Set
25
{
26
    public function create(
27
        string $variableName,
28
        bool $absolute = false,
29
        bool $withCurrencyName = true,
30
        ?string $currencyString = null,
31
        ?string $currencyVariable = null,
32
        ?string $regionString = null,
33
        ?string $regionVariable = null
34
    ): Mailcode_Commands_Command_ShowPrice
35
    {
36
        $variableName = $this->instantiator->filterVariableName($variableName);
37
38
        $paramsString = $this->compilePriceParams($absolute, $withCurrencyName,
39
            $currencyString, $currencyVariable,
40
            $regionString, $regionVariable);
41
42
        $cmd = $this->commands->createCommand(
43
            'ShowPrice',
44
            '',
45
            $variableName . $paramsString,
46
            sprintf(
47
                '{showprice: %s%s}',
48
                $variableName,
49
                $paramsString
50
            )
51
        );
52
53
        $this->instantiator->checkCommand($cmd);
54
55
        if ($cmd instanceof Mailcode_Commands_Command_ShowPrice) {
56
            return $cmd;
57
        }
58
59
        throw $this->instantiator->exceptionUnexpectedType('ShowPrice', $cmd);
60
    }
61
62
    private function compilePriceParams(
63
        bool $absolute = false,
64
        bool $withCurrencyName = true,
65
        ?string $currencyString = null,
66
        ?string $currencyVariable = null,
67
        ?string $regionString = null,
68
        ?string $regionVariable = null
69
    ): string
70
    {
71
        $params = array();
72
73
        if ($absolute) {
74
            $params[] = ' ' . Mailcode_Commands_Keywords::TYPE_ABSOLUTE;
75
        }
76
77
        if ($currencyString) {
78
            $params[] = sprintf(
79
                ' ' . CurrencyInterface::CURRENCY_PARAMETER_NAME . '=%s',
80
                $this->quoteString($currencyString)
81
            );
82
        } else if ($currencyVariable) {
83
            $params[] = sprintf(
84
                ' ' . CurrencyInterface::CURRENCY_PARAMETER_NAME . '=%s',
85
                $currencyVariable
86
            );
87
        } else if ($withCurrencyName) {
88
            $params[] = ' ' . Mailcode_Commands_Keywords::TYPE_CURRENCY_NAME;
89
        }
90
91
        if ($regionString) {
92
            $params[] = sprintf(
93
                ' ' . RegionInterface::REGION_PARAMETER_NAME . '=%s',
94
                $this->quoteString($regionString)
95
            );
96
        } else if ($regionVariable) {
97
            $params[] = sprintf(
98
                ' ' . RegionInterface::REGION_PARAMETER_NAME . '=%s',
99
                $regionVariable
100
            );
101
        }
102
103
        if (!empty($params)) {
104
            return ' ' . implode(' ', $params);
105
        }
106
107
        return '';
108
    }
109
}
110