Passed
Pull Request — master (#18)
by
unknown
09:22
created

Price   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 41
c 1
b 0
f 0
dl 0
loc 73
rs 10

2 Methods

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