Completed
Push — master ( 2dd332...d6426f )
by steef
02:53
created

AbstractSpecialDate::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 8
Ratio 30.77 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 26
ccs 19
cts 19
cp 1
rs 8.8571
cc 3
eloc 16
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Stefanius\SpecialDates\SDK;
4
5
abstract class AbstractSpecialDate implements SpecialDateInterface
6
{
7
    /**
8
     * @var \DateTime
9
     */
10
    protected $startDate;
11
12
    /**
13
     * @var \DateTime
14
     */
15
    protected $endDate;
16
17
    /**
18
     * @var \DateTime
19
     */
20
    protected $zeroDate;
21
22
    /**
23
     * @var string
24
     */
25
    protected $description;
26
27
    /**
28
     * @var int
29
     */
30
    protected $totalLength = 0;
31
32
    /**
33
     * @var bool
34
     */
35
    protected $valid = true;
36
37
    /**
38
     * @var bool
39
     */
40
    protected $bankHoliday = false;
41
42
    /**
43
     * @var bool
44
     */
45
    protected $nationalAcceptedParty = false;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $nationalAcceptedParty exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
46
47
    /**
48
     * @var string
49
     */
50
    protected $normalizedDescription = false;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $normalizedDescription exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
51
    /**
52
     * @var int
53
     */
54
    protected $year;
55
56 54
    public function __construct($year)
57
    {
58 54
        $this->year     = $year;
59 54
        $this->zeroDate = new \DateTime();
60 54
        $this->zeroDate->setDate(1, 1, 1);
61
62 54
        $this->startDate = new \DateTime();
63 54
        $this->startDate->setDate(1, 1, 1);
64
65 54
        $this->endDate = new \DateTime();
66 54
        $this->endDate->setDate(1, 1, 1);
67
68 54
        $this->generate();
69
70 54 View Code Duplication
        if ($this->startDate->format('Y') . '-' . $this->startDate->format('m') . '-' . $this->startDate->format('d') === '0001-01-01') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
71 28
            $this->valid = false;
72 28
            $this->totalLength = 0;
73 28
        }
74
75 54 View Code Duplication
        if ($this->endDate->format('Y') . '-' . $this->endDate->format('m') . '-' . $this->endDate->format('d') === '0001-01-01') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
76 28
            $this->valid = false;
77 28
            $this->totalLength = 0;
78 28
        }
79
80 54
        $this->normalizeDescription();
81 54
    }
82
83 54
    protected function normalizeDescription()
84
    {
85 54
        $string = iconv('UTF-8', 'ASCII//TRANSLIT', $this->description);
86 54
        $string = trim($string);
87 54
        $string = preg_replace("/[^a-zA-Z0-9_| -]/", ' ', $string);
88 54
        $string = preg_replace("/[| -]+/", '-', $string);
89 54
        $string = strtolower(trim($string, '-'));
90 54
        $string = preg_replace('/-{2,}/', ' ', $string);
91
92 54
        $this->normalizedDescription = $string;
93 54
    }
94
95
    /**
96
     * @param $year
97
     * @param $month
98
     * @param $day
99
     *
100
     * @return \DateTime
101
     */
102 16
    protected function generateDateTime($year, $month, $day)
103
    {
104 16
        $date = \DateTime::createFromFormat('Y-m-d', $year . '-' . $month . '-' . $day);
105
106 16
        if ($date === false) {
107
            return $this->zeroDate;
108
        }
109
110 16
        return $date;
111
    }
112
113 16
    protected function setupDateTimeObjects(\DateTime $start, \DateTime $end = null)
114
    {
115 16
        $this->startDate = $start;
116
117 16
        if ($end === null) {
118 12
            $this->endDate = $start;
119 12
        } else {
120 4
            $this->endDate = $end;
121
        }
122
123 16
        $interval = $this->startDate->diff($this->endDate);
124
125 16
        $this->totalLength = (int)$interval->format('%R%a') + 1;
126 16
    }
127
128
    /**
129
     * @return \DateTime
130
     */
131 54
    public function getStartDate()
132
    {
133 54
        if (!$this->valid || $this->startDate == null) {
134 28
            return $this->zeroDate;
135
        }
136
137 29
        return $this->startDate;
138
    }
139
140
    /**
141
     * @return \DateTime
142
     */
143 51
    public function getEndDate()
144
    {
145 51
        if (!$this->valid || $this->endDate == null) {
146 25
            return $this->zeroDate;
147
        }
148
149 26
        return $this->endDate;
150
    }
151
152
    /**
153
     * @return string
154
     */
155 51
    public function getDescription()
156
    {
157 51
        return $this->description;
158
    }
159
160
    /**
161
     * @return int
162
     */
163 51
    public function getTotalLength()
164
    {
165 51
        return $this->totalLength;
166
    }
167
168
    /**
169
     * @return bool
170
     */
171 51
    public function isValid()
172
    {
173 51
        return $this->valid;
174
    }
175
176
    /**
177
     * @return boolean
178
     */
179 51
    public function isBankHoliday()
180
    {
181 51
        return $this->bankHoliday;
182
    }
183
184
    /**
185
     * @return boolean
186
     */
187
    public function isNationalAcceptedParty()
188
    {
189
        return $this->nationalAcceptedParty;
190
    }
191
192
    /**
193
     * @return int
194
     */
195
    public function getYear()
196
    {
197
        return $this->year;
198
    }
199
200
    /**
201
     * @return string
202
     */
203 1
    public function getNormalizedDescription()
204
    {
205 1
        return $this->normalizedDescription;
206
    }
207
208
    abstract protected function generate();
209
}
210