Passed
Pull Request — master (#18)
by
unknown
03:40
created

Price   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 23
c 1
b 0
f 0
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 23 2
A compilePriceParams() 0 17 4
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\Mailcode_Commands_Command_ShowPrice;
15
use Mailcode\Mailcode_Factory_CommandSets_Set;
16
17
/**
18
 * Factory class for the `shownumber` 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(string $variableName, bool $absolute = false, bool $withCurrencyName = true): Mailcode_Commands_Command_ShowPrice
27
    {
28
        $variableName = $this->instantiator->filterVariableName($variableName);
29
        $paramsString = $this->compilePriceParams($absolute, $withCurrencyName);
30
31
        $cmd = $this->commands->createCommand(
32
            'ShowPrice',
33
            '',
34
            $variableName . $paramsString,
35
            sprintf(
36
                '{showprice: %s%s}',
37
                $variableName,
38
                $paramsString
39
            )
40
        );
41
42
        $this->instantiator->checkCommand($cmd);
43
44
        if ($cmd instanceof Mailcode_Commands_Command_ShowPrice) {
45
            return $cmd;
46
        }
47
48
        throw $this->instantiator->exceptionUnexpectedType('ShowPrice', $cmd);
49
    }
50
51
    private function compilePriceParams(bool $absolute = false, bool $withCurrencyName = true): string
52
    {
53
        $params = array();
54
55
        if ($absolute) {
56
            $params[] = ' absolute:';
57
        }
58
59
        if ($withCurrencyName) {
60
            $params[] = ' currency-name:';
61
        }
62
63
        if (!empty($params)) {
64
            return ' ' . implode(' ', $params);
65
        }
66
67
        return '';
68
    }
69
}
70