Completed
Push — master ( ee3f46...2dd99c )
by Markus
03:13
created

TimezoneRule::buildPropertyBag()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 26
rs 8.439
cc 6
eloc 13
nc 32
nop 0
1
<?php
2
3
/*
4
 * This file is part of the eluceo/iCal package.
5
 *
6
 * (c) Markus Poerschke <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Eluceo\iCal\Component;
13
14
use Eluceo\iCal\Component;
15
use Eluceo\iCal\PropertyBag;
16
use Eluceo\iCal\Property\Event\RecurrenceRule;
17
18
/**
19
 * Implementation of Standard Time and Daylight Saving Time observances (or rules)
20
 * which define the TIMEZONE component.
21
 */
22
class TimezoneRule extends Component
23
{
24
    const TYPE_DAYLIGHT = 'DAYLIGHT';
25
    const TYPE_STANDARD = 'STANDARD';
26
27
    /**
28
     * @var string
29
     */
30
    protected $type;
31
32
    /**
33
     * @var string
34
     */
35
    protected $tzOffsetFrom;
36
37
    /**
38
     * @var string
39
     */
40
    protected $tzOffsetTo;
41
42
    /**
43
     * @var string
44
     */
45
    protected $tzName;
46
47
    /**
48
     * @var \DateTime
49
     */
50
    protected $dtStart;
51
52
    /**
53
     * @var RecurrenceRule
54
     */
55
    protected $recurrenceRule;
56
57
    /**
58
     * create new Timezone Rule object by giving a rule type identifier.
59
     *
60
     * @param string $ruleType one of DAYLIGHT or STANDARD
61
     *
62
     * @throws \InvalidArgumentException
63
     */
64 View Code Duplication
    public function __construct($ruleType)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        $ruleType = strtoupper($ruleType);
67
        if ($ruleType === self::TYPE_DAYLIGHT || $ruleType === self::TYPE_STANDARD) {
68
            $this->type = $ruleType;
69
        } else {
70
            throw new \InvalidArgumentException('Invalid value for timezone rule type');
71
        }
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function buildPropertyBag()
78
    {
79
        $propertyBag = new PropertyBag();
80
81
        if ($this->getTzName()) {
82
            $propertyBag->set('TZNAME', $this->getTzName());
83
        }
84
85
        if ($this->getTzOffsetFrom()) {
86
            $propertyBag->set('TZOFFSETFROM', $this->getTzOffsetFrom());
87
        }
88
89
        if ($this->getTzOffsetTo()) {
90
            $propertyBag->set('TZOFFSETTO', $this->getTzOffsetTo());
91
        }
92
93
        if ($this->getDtStart()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getDtStart() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
94
            $propertyBag->set('DTSTART', $this->getDtStart());
95
        }
96
97
        if ($this->recurrenceRule) {
98
            $propertyBag->set('RRULE', $this->recurrenceRule);
99
        }
100
101
        return $propertyBag;
102
    }
103
104
    /**
105
     * @param $offset
106
     *
107
     * @return $this
108
     */
109
    public function setTzOffsetFrom($offset)
110
    {
111
        $this->tzOffsetFrom = $offset;
112
113
        return $this;
114
    }
115
116
    /**
117
     * @param $offset
118
     *
119
     * @return $this
120
     */
121
    public function setTzOffsetTo($offset)
122
    {
123
        $this->tzOffsetTo = $offset;
124
125
        return $this;
126
    }
127
128
    /**
129
     * @param $name
130
     *
131
     * @return $this
132
     */
133
    public function setTzName($name)
134
    {
135
        $this->tzName = $name;
136
137
        return $this;
138
    }
139
140
    /**
141
     * @param \DateTime $dtStart
142
     *
143
     * @return $this
144
     */
145
    public function setDtStart(\DateTime $dtStart)
146
    {
147
        $this->dtStart = $dtStart;
148
149
        return $this;
150
    }
151
152
    /**
153
     * @param RecurrenceRule $recurrenceRule
154
     *
155
     * @return $this
156
     */
157
    public function setRecurrenceRule(RecurrenceRule $recurrenceRule)
158
    {
159
        $this->recurrenceRule = $recurrenceRule;
160
161
        return $this;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function getType()
168
    {
169
        return $this->type;
170
    }
171
172
    /**
173
     * @return string
174
     */
175
    public function getTzOffsetFrom()
176
    {
177
        return $this->tzOffsetFrom;
178
    }
179
180
    /**
181
     * @return string
182
     */
183
    public function getTzOffsetTo()
184
    {
185
        return $this->tzOffsetTo;
186
    }
187
188
    /**
189
     * @return string
190
     */
191
    public function getTzName()
192
    {
193
        return $this->tzName;
194
    }
195
196
    /**
197
     * @return RecurrenceRule
198
     */
199
    public function getRecurrenceRule()
200
    {
201
        return $this->recurrenceRule;
202
    }
203
204
    /**
205
     * @return mixed return string representation of start date or NULL if no date was given
206
     */
207
    public function getDtStart()
208
    {
209
        if ($this->dtStart) {
210
            return $this->dtStart->format('Ymd\THis');
211
        }
212
213
        return;
214
    }
215
}
216