Test Failed
Push — master ( 9ff364...742ef2 )
by Sebastian
03:58
created

Number::compileNumberParams()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 23
rs 9.9332
1
<?php
2
/**
3
 * File containing the class {@see \Mailcode\Factory\CommandSets\Set\Show\Number}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Factory
7
 * @see \Mailcode\Factory\CommandSets\Set\Show\Number
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Factory\CommandSets\Set\Show;
13
14
use Mailcode\Mailcode_Commands_Command_ShowNumber;
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 Sebastian Mordziol <[email protected]>
23
 */
24
class Number extends Mailcode_Factory_CommandSets_Set
25
{
26
    public function create(string $variableName, string $formatString="", bool $absolute=false) : Mailcode_Commands_Command_ShowNumber
27
    {
28
        $variableName = $this->instantiator->filterVariableName($variableName);
29
        $paramsString = $this->compileNumberParams($formatString, $absolute);
30
31
        $cmd = $this->commands->createCommand(
32
            'ShowNumber',
33
            '',
34
            $variableName.$paramsString,
35
            sprintf(
36
                '{shownumber: %s%s}',
37
                $variableName,
38
                $paramsString
39
            )
40
        );
41
42
        $this->instantiator->checkCommand($cmd);
43
44
        if($cmd instanceof Mailcode_Commands_Command_ShowNumber)
45
        {
46
            return $cmd;
47
        }
48
49
        throw $this->instantiator->exceptionUnexpectedType('ShowNumber', $cmd);
50
    }
51
52
    private function compileNumberParams(string $formatString="", bool $absolute=false) : string
53
    {
54
        $params = array();
55
56
        if(!empty($formatString))
57
        {
58
            $params[] = sprintf(
59
                ' "%s"',
60
                $formatString
61
            );
62
        }
63
64
        if($absolute)
65
        {
66
            $params[] = ' absolute:';
67
        }
68
69
        if(!empty($params))
70
        {
71
            return ' '.implode(' ', $params);
72
        }
73
74
        return '';
75
    }
76
}
77