DistanceUnit::conversionRateToMeters()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 12
nc 5
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Domain\Geolocation;
12
13
use Cubiche\Domain\System\Enum;
14
15
/**
16
 * Distance Unit Enum.
17
 *
18
 * @method DistanceUnit METER()
19
 * @method DistanceUnit FOOT()
20
 * @method DistanceUnit KILOMETER()
21
 * @method DistanceUnit MILE()
22
 *
23
 * @author Karel Osorio Ramírez <[email protected]>
24
 */
25
final class DistanceUnit extends Enum
26
{
27
    const FOOT = 'ft';
28
    const METER = 'm';
29
    const KILOMETER = 'km';
30
    const MILE = 'mi';
31
32
    /**
33
     * @param DistanceUnit $unit
34
     *
35
     * @return float
36
     */
37
    public function conversionRate(DistanceUnit $unit)
38
    {
39
        if ($this->equals($unit)) {
40
            return 1;
41
        }
42
43
        return $this->conversionRateToMeters() / $unit->conversionRateToMeters();
44
    }
45
46
    /**
47
     * @return float
48
     */
49
    private function conversionRateToMeters()
50
    {
51
        switch ($this->value) {
52
            case self::METER:
53
                return 1;
54
            case self::KILOMETER:
55
                return 1000;
56
            case self::MILE:
57
                return 1609.344;
58
            case self::FOOT:
59
                return 0.3048;
60
            default:
61
                return 1;
62
        }
63
    }
64
}
65