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

Temperature   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 73.91%

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 17
cts 23
cp 0.7391
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A celsius() 0 3 1
A __toString() 0 6 1
A roundCelsius() 0 3 1
A getScale() 0 3 1
A getDegrees() 0 8 3
A fahrenheit() 0 3 1
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