AbstractDateTimeTransformer::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 13
nc 6
nop 2
crap 6
1
<?php
2
3
namespace TreeHouse\Feeder\Modifier\Data\Transformer;
4
5
use TreeHouse\Feeder\Exception\UnexpectedTypeException;
6
7
abstract class AbstractDateTimeTransformer implements TransformerInterface
8
{
9
    /**
10
     * The name of the input timezone.
11
     *
12
     * @var string
13
     */
14
    protected $inputTimezone;
15
16
    /**
17
     * The name of the output timezone.
18
     *
19
     * @var string
20
     */
21
    protected $outputTimezone;
22
23
    /**
24
     * @param string $inputTimezone  The name of the input timezone
25
     * @param string $outputTimezone The name of the output timezone
26
     *
27
     * @throws UnexpectedTypeException   If a timezone is not a string
28
     * @throws \InvalidArgumentException If a timezone is invalid
29
     */
30 66
    public function __construct($inputTimezone = null, $outputTimezone = null)
31
    {
32
        $timeZones = [
33 66
            'inputTimezone' => $inputTimezone,
34 66
            'outputTimezone' => $outputTimezone,
35 66
        ];
36
37 66
        foreach ($timeZones as $field => $timezone) {
38 66
            if (!is_string($timezone) && null !== $timezone) {
39 2
                throw new UnexpectedTypeException($timezone, 'string');
40
            }
41
42 64
            $timezone = $timezone ?: date_default_timezone_get();
43
44
            // Check if input and output timezones are valid
45
            try {
46 64
                new \DateTimeZone($timezone);
47 64
            } catch (\Exception $e) {
48 4
                throw new \InvalidArgumentException(sprintf('%s is invalid: %s.', $field, $timezone), $e->getCode(), $e);
49
            }
50
51 62
            $this->$field = $timezone;
52 62
        }
53 60
    }
54
}
55