Passed
Pull Request — master (#17)
by
unknown
03:33
created

Date::getFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * File containing the class {@see \Mailcode\Factory\CommandSets\Set\Show\Date}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Factory
7
 * @see \Mailcode\Factory\CommandSets\Set\Show\Date
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Factory\CommandSets\Set\Show;
13
14
use Mailcode\Interfaces\Commands\Validation\TimezoneInterface;
15
use Mailcode\Mailcode_Commands_Command_ShowDate;
16
use Mailcode\Mailcode_Factory_CommandSets_Set;
17
18
/**
19
 * Factory class for the `showdate` command.
20
 *
21
 * @package Mailcode
22
 * @subpackage Factory
23
 * @author Sebastian Mordziol <[email protected]>
24
 */
25
class Date extends Mailcode_Factory_CommandSets_Set
26
{
27
    public function create(string $variableName, string $formatString = "", string $timezoneString = null, string $timezoneVariable = null): Mailcode_Commands_Command_ShowDate
28
    {
29
        $variableName = $this->instantiator->filterVariableName($variableName);
30
31
        $format = $this->getFormat($formatString);
32
        $timezone = $this->getTimezone($timezoneString, $timezoneVariable);
33
34
        $cmd = $this->commands->createCommand(
35
            'ShowDate',
36
            '',
37
            $variableName . $format . $timezone,
38
            sprintf(
39
                '{showdate: %s%s%s}',
40
                $variableName,
41
                $format,
42
                $timezone
43
            )
44
        );
45
46
        $this->instantiator->checkCommand($cmd);
47
48
        if ($cmd instanceof Mailcode_Commands_Command_ShowDate) {
49
            return $cmd;
50
        }
51
52
        throw $this->instantiator->exceptionUnexpectedType('ShowDate', $cmd);
53
    }
54
55
    public function now(string $formatString = "", string $timezoneString = null, string $timezoneVariable = null): Mailcode_Commands_Command_ShowDate
56
    {
57
        $format = $this->getFormat($formatString);
58
        $timezone = $this->getTimezone($timezoneString, $timezoneVariable);
59
60
        $cmd = $this->commands->createCommand(
61
            'ShowDate',
62
            '',
63
            $format . $timezone,
64
            sprintf(
65
                '{showdate: %s%s}',
66
                $format,
67
                $timezone
68
            )
69
        );
70
71
        $this->instantiator->checkCommand($cmd);
72
73
        if ($cmd instanceof Mailcode_Commands_Command_ShowDate) {
74
            return $cmd;
75
        }
76
77
        throw $this->instantiator->exceptionUnexpectedType('ShowDate', $cmd);
78
    }
79
80
    /**
81
     * @param string $formatString
82
     * @return string
83
     */
84
    private function getFormat(string $formatString): string
85
    {
86
        $format = '';
87
        if (!empty($formatString)) {
88
            $format = sprintf(
89
                ' "%s"',
90
                $formatString
91
            );
92
        }
93
        return $format;
94
    }
95
96
    /**
97
     * @param string|null $timezoneString
98
     * @param string|null $timezoneVariable
99
     * @return string
100
     */
101
    private function getTimezone(?string $timezoneString, ?string $timezoneVariable): string
102
    {
103
        $timezone = '';
104
        if (!empty($timezoneString)) {
105
            $timezone = sprintf(
106
                ' ' . TimezoneInterface::PARAMETER_NAME . '=%s',
107
                $this->quoteString($timezoneString)
108
            );
109
        } else if (!empty($timezoneVariable)) {
110
            $timezone = sprintf(
111
                ' ' . TimezoneInterface::PARAMETER_NAME . '=%s',
112
                $timezoneVariable
113
            );
114
        }
115
        return $timezone;
116
    }
117
}
118