Passed
Push — master ( f9c14b...c6fe8b )
by Laurens
02:03
created

Temperature::getScale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\NestApi\Client\Device\Thermostat;
6
7
use LauLamanApps\NestApi\Client\Device\Thermostat\Temperature\Scale;
8
9
final class Temperature
10
{
11
    /**
12
     * @var Scale
13
     */
14
    private $scale;
15
16
    /**
17
     * @var float
18
     */
19
    private $degrees;
20
21 17
    public function __construct(Scale $scale, float $degrees)
22
    {
23 17
        $this->scale = $scale;
24 17
        $this->degrees = $degrees;
25 17
    }
26
27 10
    public static function celsius(float $degrees): self
28
    {
29 10
        return new self(Scale::celsius(), self::roundCelsius($degrees));
30
    }
31
32 5
    public static function fahrenheit(int $degrees): self
33
    {
34 5
        return new self(Scale::fahrenheit(), (float)$degrees);
35
    }
36
37 11
    public function getScale(): Scale
38
    {
39 11
        return $this->scale;
40
    }
41
42
    /**
43
     * @return float|int
44
     */
45 13
    public function getDegrees()
46
    {
47 13
        if ($this->scale->isFahrenheit()) {
48 6
            return (int) $this->degrees;
49
        }
50
51 7
        if ($this->scale->isCelsius()) {
52 7
            return self::roundCelsius($this->degrees);
53
        }
54
    }
55
56 11
    private static function roundCelsius(float $degrees): float
57
    {
58 11
        return round($degrees * 2) / 2;
59
    }
60
61
    public function __toString(): string
62
    {
63
        return sprintf(
64
            '%d°%s',
65
            $this->getDegrees(),
66
            strtoupper($this->scale->getValue())
67
        );
68
    }
69
}
70