Passed
Pull Request — master (#15)
by Sebastian
04:12
created

TimezoneTrait::setTimezone()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 12
nop 1
dl 0
loc 23
rs 9.5222
1
<?php
2
/**
3
 * File containing the trait {@see \Mailcode\Traits\Commands\Validation\TimezoneTrait}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Validation
7
 * @see \Mailcode\Traits\Commands\Validation\TimezoneTrait
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Traits\Commands\Validation;
13
14
use Mailcode\Interfaces\Commands\Validation\TimezoneInterface;
15
use Mailcode\Mailcode_Commands_Keywords;
16
use Mailcode\Mailcode;
17
use Mailcode\Mailcode_Commands_Command_ShowDate;
18
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token;
19
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral;
20
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_Variable;
21
use Mailcode\Mailcode_Variables_Variable;
22
use function Mailcode\t;
23
24
/**
25
 * @package Mailcode
26
 * @subpackage Validation
27
 * @author Olaf Böcker <[email protected]>
28
 *
29
 * @see TimezoneInterface
30
 */
31
trait TimezoneTrait
32
{
33
    private bool $timezoneEnabled = false;
34
35
    private ?Mailcode_Parser_Statement_Tokenizer_Token $timezoneToken = null;
36
37
    protected function validateSyntax_check_timezone(): void
38
    {
39
        $this->timezoneToken = $this->requireParams()
0 ignored issues
show
Bug introduced by
It seems like requireParams() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        $this->timezoneToken = $this->/** @scrutinizer ignore-call */ requireParams()
Loading history...
40
            ->getInfo()
41
            ->getTokenByParamName(TimezoneInterface::PARAMETER_NAME);
42
43
        if ($this->timezoneToken === null) {
44
            return;
45
        }
46
47
        if (!$this->timezoneToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral &&
48
            !$this->timezoneToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) {
49
            $this->validationResult->makeError(
50
                t('Invalid timezone:') . ' ' . t('Expected a string or variable.'),
51
                TimezoneInterface::VALIDATION_TIMEZONE_CODE_WRONG_TYPE
52
            );
53
        }
54
    }
55
56
    /**
57
     * Gets the time zone to use for the command. If none has
58
     * been specified in the original command, the default
59
     * time zone is used as defined via {@see Mailcode_Commands_Command_ShowDate::setDefaultTimezone()}.
60
     *
61
     * @return Mailcode_Parser_Statement_Tokenizer_Token
62
     */
63
    public function getTimezoneToken(): Mailcode_Parser_Statement_Tokenizer_Token
64
    {
65
        if(!isset($this->timezoneToken)) {
66
            $this->timezoneToken = $this->createTimeZoneToken();
67
        }
68
69
        return $this->timezoneToken;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->timezoneToken could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Parser_Statement_Tokenizer_Token. Consider adding an additional type-check to rule them out.
Loading history...
70
    }
71
72
    /**
73
     * Creates the default time zone token on demand.
74
     *
75
     * @return Mailcode_Parser_Statement_Tokenizer_Token
76
     */
77
    private function createTimeZoneToken() : Mailcode_Parser_Statement_Tokenizer_Token
78
    {
79
        $default = Mailcode_Commands_Command_ShowDate::getDefaultTimezone();
80
        $info = $this->requireParams()->getInfo();
81
82
        if($default instanceof Mailcode_Variables_Variable) {
83
            $token = $info->addVariable($default);
84
        } else {
85
            $token = $info->addStringLiteral($default);
86
        }
87
88
        $info->setParamName($token, TimezoneInterface::PARAMETER_NAME);
89
90
        return $token;
91
    }
92
93
    /**
94
     * @param Mailcode_Variables_Variable|string|NULL $timezone
95
     * @return $this
96
     */
97
    public function setTimezone($timezone) : self
98
    {
99
        $info = $this->requireParams()->getInfo();
100
        $token = null;
101
102
        $existing = $info->getTokenByParamName(TimezoneInterface::PARAMETER_NAME);
103
        if($existing) {
104
            $info->removeToken($existing);
105
        }
106
107
        if($timezone instanceof Mailcode_Variables_Variable) {
108
            $token = $info->addVariable($timezone);
109
        } else if(is_string($timezone)) {
110
            $token = $info->addStringLiteral($timezone);
111
        }
112
113
        if($token !== null) {
114
            $info->setParamName($token, TimezoneInterface::PARAMETER_NAME);
115
        }
116
117
        $this->timezoneToken = $token;
118
119
        return $this;
120
    }
121
}
122